MvvmCross可绑定MonoTouch.Dialog放射性元素 [英] MvvmCross Bindable MonoTouch.Dialog RadioElement

查看:215
本文介绍了MvvmCross可绑定MonoTouch.Dialog放射性元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个可绑定放射性元素在MvvmCross一个MonoTouch.Dialog实施使用,并在之后的 https://github.com/MvvmCross/MvvmCross-Tutorials/tree/master/DialogExamples/DialogExamples.Touch/BindableElements (如建议的 MvvmCross Monotouch.Dialog数据绑定到表),我创建了以下类:

 公共类MvxBindableRadioElement:放射性元素,IBindableElement 
{
公共IMvxBindingContext的BindingContext {搞定;组; }

公共MvxBindableRadioElement()
{
this.CreateBindingContext();

this.DelayBind(()=> {
VAR集= this.CreateBindingSet< MvxBindableRadioElement,PropertyCategory>();
set.Bind()对(我= > me.Caption)。要(p值=> p.Id);
set.Bind()为(我=方式> me.Value)。要(p值=> p.Value);
set.Apply();
});
}

保护覆盖无效的Dispose(BOOL处置)
{
如果(处置){
BindingContext.ClearAllBindings();
}

base.Dispose(处置);
}

公共虚拟对象的DataContext
{
{返回BindingContext.DataContext; }
集合{BindingContext.DataContext =价值; }
}
}



PropertyCategory 是一个基本模型:

 公共类PropertyCategory 
{
公众诠释标识{得到;组; }
公共字符串值{获得;组; }
}

这是使用方法如下:

 新科{
新rootElement的(类别,新RadioGroup中()){
新BindableSection< MvxBindableRadioElement> ().Bind(绑定,E => e.ItemsSource,VM = GT; vm.PropertyCategories)
} .Bind(绑定,E => e.RadioSelected,VM = GT; vm.PropertyCategory)作为元素
}

其中, BindableSection 从上述MvvmCross回购服用。



调试我能够确认<$的 newElements 变量C $ C> MvxBindableSection 正确填入 MvxBindableRadioElement S,但出现以下错误时, TableView.ReloadData() 行执行:

  MvxBind:错误:12.12问题绑定的ItemsSource为PropertyCategories结合执行过程中看到 - 问题TargetInvocationException:异常已被调用的目标引发异常。在System.Reflection.MonoMethod.Invoke 
(OBJ System.Object的,的BindingFlags invokeAttr,System.Reflection.Binder粘结剂,System.Object的[]参数,System.Globalization.CultureInfo文化)[0x0005c]在/ Developer / MonoTouch的/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:238
在System.Reflection.MethodBase.Invoke(System.Object的OBJ,System.Object的[]参数)[0x00000]在/开发商/ MonoTouch的/来源/单声道/ MCS /班/ corlib /的System.Reflection / MethodBase.cs:114
在Cirrious.MvvmCross.Binding.Bindings.Target.MvxPropertyInfoTargetBinding.SetValueImpl(System.Object的目标,System.Object的值)[0x00000]上述<文件名不明>:在Cirrious.MvvmCross.Binding.Bindings.Target.MvxConvertingTargetBinding.SetValue(System.Object的价值0
)[0x00000]上述<文件名不明> 0 $ b在Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding.UpdateTargetFromSource(System.Object的价值)$ b [0x00000]上述<文件名不明> 0
的InnerException是的NullReferenceException:未将对象引用设置到对象$实例b $ b。在<&删节GT; .iOS.Views.MvxBindableSection`1 [<&删节GT; .Views.MvxBindableRadioElement] .NotifyDataSetChanged()[0x000b9]上述<&删节GT; /MonoTouch.Dialog/MvxBindableSection.cs:87
AT<&删节GT; .iOS.Views.MvxBindableSection`1 [<&删节GT; .Views.MvxBindableRadioElement] .SetItemsSource(IEnumerable的值)[0x00094]上述<&删节GT; /MonoTouch.Dialog/MvxBindableSection.cs:$ 56 b $ b。在<&删节GT; .iOS.Views.MvxBindableSection`1 [<&删节GT; .Views.MvxBindableRadioElement]在[0x00003] .set_ItemsSource(IEnumerable的值)LT;删节> /MonoTouch.Dialog/MvxBindableSection.cs:30在(包装管理到本机)
System.Reflection.MonoMethod:InternalInvoke(System.Reflection.MonoMethod,对象,对象[],System.Exception的&安培;)在System.Reflection.MonoMethod.Invoke
(System.Object的OBJ,的BindingFlags invokeAttr,System.Reflection.Binder粘结剂,System.Object的[]参数,System.Globalization.CultureInfo文化)[0x00044]在/ Developer / MonoTouch的/来源/单声道/ MCS /班/ corlib /系统.Reflection / MonoMethod.cs:230

此故障排除,我更换了 MvxBindableRadioElement CustomStringElement 从上面提到的MvvmCross回购:

 新MvxBindableSection< CustomStringElement>()绑定(绑定元素=> element.ItemsSource,VM => vm.PropertyCategories)

这就像一个魅力。为什么在 CustomStringElement 的工作,而不是 MvxBindableRadioElement ?我一定要创建一个可绑定 RadioGroup中封装了 MvxBindableRadioElement S'



编辑:



下面是内部异常(的NullReferenceException):

 在CrossUI.Touch.Dialog.Elements.RadioElement.SubscribeToRoot()[0x00000]上述<文件名不明>:在CrossUI.Touch.Dialog.Elements.RadioElement.GetCellImpl 0 
(MonoTouch.UIKit .UITableView电视)[0x00000]上述<文件名不明> 0
在CrossUI.Touch.Dialog.Elements.Element.GetCell(MonoTouch.UIKit.UITableView TV)[0x00000]上述<文件名不明>:0
在CrossUI.Touch.Dialog.DialogViewController + Source.GetCell(MonoTouch.UIKit.UITableView的tableView,MonoTouch.Foundation.NSIndexPath indexPath)[0x00000]上述<文件名不明> 0
。在(包装管理-to-本地)MonoTouch.UIKit.UIApplication:UIApplicationMain(整型,字符串[],IntPtr的,IntPtr的)
在MonoTouch.UIKit.UIApplication.Main(System.String [] ARGS,System.String principalClassName,系统。串delegateClassName)[0x0004c]在PGPCapture.iOS.Application.Main /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
(System.String []参数)[0x00008]上述< ;节录> /Main.cs:21


解决方案

确定这样这个问题是与 BindableSection (摘自的 MvvmCross-教程/ DialogExamples ):
中的部分元素的未设置(即为),除非您使用添加元素的addAll()。这个问题实际上是发生在MT.Dialog本身的肠子。



更新 MvxBindableSection 可用的这里


I am attempting to create a bindable RadioElement to use in a MonoTouch.Dialog implementation in MvvmCross, and following the pattern in https://github.com/MvvmCross/MvvmCross-Tutorials/tree/master/DialogExamples/DialogExamples.Touch/BindableElements (as suggested in MvvmCross Monotouch.Dialog binding data to a table), I created the following class:

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 is a basic Model:

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

This is used as follows:

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
}

where BindableSection is taken from the MvvmCross repo mentioned above.

Debugging I am able to verify that the newElements variable of MvxBindableSection is correctly populated with MvxBindableRadioElements, but the following error occurs when the TableView.ReloadData() line is executed:

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

Troubleshooting this, I replaced the MvxBindableRadioElement with CustomStringElement from the MvvmCross repo mentioned above:

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

This works like a charm. Why does the CustomStringElement work, but not theMvxBindableRadioElement? Do I have to create a bindable RadioGroup that wraps the MvxBindableRadioElements?

EDIT:

Here is the Inner Exception (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 

解决方案

OK so the issue was with the implementation of BindableSection (taken from MvvmCross-Tutorials / DialogExamples): the Section element's Parent is not set (i.e. is null) unless you add the elements using AddAll(). This issue actually occurs in the bowels of MT.Dialog itself.

The updated MvxBindableSection is available here.

这篇关于MvvmCross可绑定MonoTouch.Dialog放射性元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆