gpt4 book ai didi

android - MVVM 交叉绑定(bind)崩溃 Android 应用程序

转载 作者:太空狗 更新时间:2023-10-29 16:39:52 27 4
gpt4 key购买 nike

我有一个基于 Xamarin 和 MvvmCross 的 Android 应用程序。在该应用程序中,有一个带有我自己创建的 ExpandableListView 的 View 。现在这个列表显示了几个项目,它们使用 MvvmCross 绑定(bind)到它们的 DataContext。但是,由于各个 ListItemView 的 View 差异很大,因此部分 ListItemView 是在 ExpandedListViewAdapter 中以编程方式生成的。这就像这样:

        public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
{
object child = GetRawChild(groupPosition, childPosition);

if (child == null)
{
MvxBindingTrace.Trace(MvxTraceLevel.Error, "GetView called for group that seems to have no itemssource: it is null");
return null;
}

var view = (MvxListItemView)GetBindableView(convertView, child, ChildItemTemplateId);
var placeholder = view.FindViewById<BindableFrameLayout>(Resource.Id.placeholder);

var questionVm = (QuestionViewModel)child;

if(questionVm.ViewType == "TextBox")
{
placeholder.RemoveAllViews();

var text = new BindableEditText(context);

text.InputType = InputType;
text.SetRawInputType(InputType);
placeholder.RemoveAllViews();
placeholder.AddView(text);

var answer = questionVm.Children.First();
text.DataContext = answer;
var binding = text.CreateInlineBindingTarget<AnswerViewModel>();
text.Bind(binding, et => et.Text, vm => vm.Model.Value, (string)null, null, null,
MvxBindingMode.TwoWay);
}
else if(questionVm.ViewType == "Spinner")
{
placeholder.RemoveAllViews();

MvxSpinner spinner = new MvxSpinner(context, null);
spinner.ItemsSource = questionVm.Children;
spinner.ItemSelected += (sender, args) =>
{
for (int i = 0; i < questionVm.Children.Count; i++)
{
var answer = (IAnswerViewModel)questionVm.Children[i];
if (i == spinner.SelectedItemPosition)
answer.IsSelected = true;
else
answer.IsSelected = false;
}
};
spinner.Bind(bindings, ctrl => ctrl.ItemsSource, vm => vm.Children, (string)null, null, null, MvxBindingMode.OneWay);
var chosenAnswer = questionVm.Children.Cast<IAnswerViewModel>().FirstOrDefault(@a => @a.IsSelected == true);
if (chosenAnswer != null)
spinner.SetSelection(questionVm.Children.Cast<IAnswerViewModel>().ToList().IndexOf(chosenAnswer));
placeholder.AddView(spinner);
}

... 和“BindableEditText”如下所示: 使用系统; 使用 Android.Content; 使用 Android.Runtime; 使用 Android.Views; 使用 Android.Widget; 使用 Android.Util; 使用 Cirrious.MvvmCross.Binding.Droid.BindingContext; 使用 Cirrious.MvvmCross.Binding.BindingContext;

        namespace iCL.Filler.Droid.Controls
{
public class BindableEditText : EditText, IMvxBindingContextOwner
{
private readonly IMvxAndroidBindingContext _bindingContext;

public BindableEditText(Context context)
: base(context)
{
_bindingContext = new MvxAndroidBindingContext(context, null);
}

public BindableEditText(Context context, IAttributeSet attributes)
: base(context, attributes)
{
_bindingContext = new MvxAndroidBindingContext(context, null);
}

public BindableEditText(Context context, IAttributeSet attributes, int defStyle)
: base(context, attributes, defStyle)
{
_bindingContext = new MvxAndroidBindingContext(context, null);
}

public BindableEditText(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
{
}

protected IMvxAndroidBindingContext AndroidBindingContext
{
get { return _bindingContext; }
}

public IMvxBindingContext BindingContext
{
get { return _bindingContext; }
set { throw new NotImplementedException("BindingContext is readonly in the radio button"); }
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
this.BindingContext.ClearAllBindings();
}

base.Dispose(disposing);
}

public override void SetText(Java.Lang.ICharSequence text, TextView.BufferType type)
{
try
{
base.SetText(text, type);
}
catch (Exception ex)
{

}
}

protected View Content { get; set; }

public object DataContext
{
get { return _bindingContext.DataContext; }
set { _bindingContext.DataContext = value; }
}
}
}

所以我的问题是,偶尔,当我滚动并单击我的 ListView 时,我会收到如下所示的运行时错误,并且我的应用程序“崩溃”,这实际上意味着它导航回上一个屏幕。

10-29 14:04:37.140 D/dalvikvm( 5989): GC_EXPLICIT freed 751K, 11% free 11369K/12679K, paused 0ms+1ms
10-29 14:04:39.970 D/dalvikvm( 5989): GC_FOR_ALLOC freed 715K, 16% free 10692K/12679K, paused 5ms
10-29 14:04:41.831 E/mono-rt ( 5989): Stacktrace:
10-29 14:04:41.831 E/mono-rt ( 5989):
10-29 14:04:41.831 E/mono-rt ( 5989): at <unknown> <0xffffffff>
10-29 14:04:41.831 E/mono-rt ( 5989): at (wrapper managed-to-native) object.wrapper_native_0xb71f1820 (intptr,intptr,intptr,intptr,Android.Runtime.JValue[]) <IL 0x00124, 0xffffffff>
10-29 14:04:41.831 E/mono-rt ( 5989): at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) [0x00000] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.2-branch/a25a31d0/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:612
10-29 14:04:41.831 E/mono-rt ( 5989): at Android.Widget.CompoundButton.set_Checked (bool) [0x00070] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.2-branch/a25a31d0/source/monodroid/src/Mono.Android/platforms/android-14/src/generated/Android.Widget.CompoundButton.cs:255
10-29 14:04:41.831 E/mono-rt ( 5989): at (wrapper runtime-invoke) <Module>.runtime_invoke_void__this___byte (object,intptr,intptr,intptr) <IL 0x00054, 0xffffffff>
10-29 14:04:41.831 E/mono-rt ( 5989): at <unknown> <0xffffffff>
10-29 14:04:41.831 E/mono-rt ( 5989): at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&) <IL 0x00030, 0xffffffff>
10-29 14:04:41.831 E/mono-rt ( 5989): at System.Reflection.MonoMethod.Invoke (object,System.Reflection.BindingFlags,System.Reflection.Binder,object[],System.Globalization.CultureInfo) <IL 0x0004a, 0x0016f>
10-29 14:04:41.831 E/mono-rt ( 5989): at System.Reflection.MethodBase.Invoke (object,object[]) <IL 0x00006, 0x00048>
10-29 14:04:41.831 E/mono-rt ( 5989): at Cirrious.MvvmCross.Binding.Bindings.Target.MvxPropertyInfoTargetBinding.SetValueImpl (object,object) <IL 0x0001a, 0x000a7>
10-29 14:04:41.831 E/mono-rt ( 5989): at Cirrious.MvvmCross.Binding.Bindings.Target.MvxConvertingTargetBinding.SetValue (object) <IL 0x0008c, 0x002b1>
The program 'Mono' has exited with code 0 (0x0).

不知道是什么问题....会不会是某个 Java 对象已经完成,而我的绑定(bind)试图调用它?

最佳答案

遵循提供的代码 fragment 有点困难,但我猜你看到的问题是由于你正在回收 cell 并且每次回收它时你是:

  • 创建一个新的单元格 subview ,
  • 创建一个新的绑定(bind),
  • 清除旧的 subview
  • 删除旧绑定(bind)。

解决此问题的一种方法可能是使用 WithClearBindingKey 流畅的方法来清除这些绑定(bind)。

例如,如果绑定(bind)创建为:

 var set = this.CreateBindingSet<Cell, CellViewModel>();
set.Bind(text).To(vm => vm.TextValue).WithClearBindingKey("MyDynamicBindings");
set.Apply();

然后可以使用以下方法清除使用此标记创建的绑定(bind):

 this.ClearBindings("MyDynamicBindings");

关于android - MVVM 交叉绑定(bind)崩溃 Android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19659458/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com