【问题标题】:MvvmCross Bindable MonoTouch.Dialog RadioElementMvvmCross 可绑定 MonoTouch.Dialog RadioElement
【发布时间】:2014-07-04 22:34:03
【问题描述】:

我正在尝试创建一个可绑定的 RadioElement 以在 MvvmCross 中的 MonoTouch.Dialog 实现中使用,并按照https://github.com/MvvmCross/MvvmCross-Tutorials/tree/master/DialogExamples/DialogExamples.Touch/BindableElements 中的模式(如MvvmCross Monotouch.Dialog binding data to a table 中的建议),我创建了以下类:

public class MvxBindableRadioElement : RadioElement, IBindableElement
{
    public IMvxBindingContext BindingContext { get; set; }

    public MvxBindableRadioElement ()
    {
        this.CreateBindingContext();

        this.DelayBind(() => {
            var set = this.CreateBindingSet<MvxBindableRadioElement, PropertyCategory>();
            set.Bind().For(me => me.Caption).To(p => p.Id);
            set.Bind().For(me => me.Value).To(p => p.Value);
            set.Apply();
        });
    }

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

        base.Dispose(disposing);
    }

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

PropertyCategory 是一个基本模型:

public class PropertyCategory
{
    public int Id { get; set; }
    public string Value { get; set; }
}

这个用法如下:

new Section {
    new RootElement ("Category", new RadioGroup ()) {
        new BindableSection<MvxBindableRadioElement> ().Bind(bindings, e => e.ItemsSource, vm => vm.PropertyCategories)
    }.Bind (bindings, e => e.RadioSelected, vm => vm.PropertyCategory) as Element
}

其中BindableSection 取自上述 MvvmCross 存储库。

调试我能够验证MvxBindableSectionnewElements变量是否正确填充了MvxBindableRadioElements,但是在执行TableView.ReloadData()行时会出现以下错误:

MvxBind:Error: 12.12 Problem seen during binding execution for binding ItemsSource for PropertyCategories - problem TargetInvocationException: Exception has been thrown by the target of an invocation.
at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0005c] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:238 
at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MethodBase.cs:114 
at Cirrious.MvvmCross.Binding.Bindings.Target.MvxPropertyInfoTargetBinding.SetValueImpl (System.Object target, System.Object value) [0x00000] in <filename unknown>:0 
at Cirrious.MvvmCross.Binding.Bindings.Target.MvxConvertingTargetBinding.SetValue (System.Object value) [0x00000] in <filename unknown>:0 
at Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding.UpdateTargetFromSource (System.Object value) [0x00000] in <filename unknown>:0 
InnerException was NullReferenceException: Object reference not set to an instance of an object
at <redacted>.iOS.Views.MvxBindableSection`1[<redacted>.Views.MvxBindableRadioElement].NotifyDataSetChanged () [0x000b9] in <redacted>/MonoTouch.Dialog/MvxBindableSection.cs:87 
at <redacted>.iOS.Views.MvxBindableSection`1[<redacted>.Views.MvxBindableRadioElement].SetItemsSource (IEnumerable value) [0x00094] in <redacted>/MonoTouch.Dialog/MvxBindableSection.cs:56 
at <redacted>.iOS.Views.MvxBindableSection`1[<redacted>.Views.MvxBindableRadioElement].set_ItemsSource (IEnumerable value) [0x00003] in <redacted>/MonoTouch.Dialog/MvxBindableSection.cs:30 
at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00044] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:230

为了解决这个问题,我将上面提到的 MvvmCross 存储库中的 MvxBindableRadioElement 替换为 CustomStringElement

new MvxBindableSection<CustomStringElement>().Bind(bindings, element => element.ItemsSource, vm => vm.PropertyCategories)

这就像一个魅力。为什么CustomStringElement 有效,而MvxBindableRadioElement 无效?我是否必须创建一个可绑定的RadioGroup 来包装MvxBindableRadioElements?

编辑:

这里是内部异常(NullReferenceException):

at CrossUI.Touch.Dialog.Elements.RadioElement.SubscribeToRoot () [0x00000] in <filename unknown>:0 
at CrossUI.Touch.Dialog.Elements.RadioElement.GetCellImpl (MonoTouch.UIKit.UITableView tv) [0x00000] in <filename unknown>:0 
at CrossUI.Touch.Dialog.Elements.Element.GetCell (MonoTouch.UIKit.UITableView tv) [0x00000] in <filename unknown>:0 
at CrossUI.Touch.Dialog.DialogViewController+Source.GetCell (MonoTouch.UIKit.UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath) [0x00000] in <filename unknown>:0 
at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38 
at PGPCapture.iOS.Application.Main (System.String[] args) [0x00008] in <redacted>/Main.cs:21 

【问题讨论】:

    标签: c# xamarin.ios mvvmcross monotouch.dialog


    【解决方案1】:

    好的,所以问题在于BindableSection 的实现(取自MvvmCross-Tutorials / DialogExamples): Section 元素的 Parent 未设置(即为 null),除非您使用 AddAll() 添加元素。这个问题实际上发生在 MT.Dialog 本身的内部。

    更新后的MvxBindableSection 可用here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-18
      • 2013-10-17
      相关资源
      最近更新 更多