Mvvmcross绑定失败与"使用共享运行时"未经检查Xamarin.Android

时间:2014-09-16 02:41:56

标签: xamarin.android mvvmcross

我有一个在Debug构建中运行良好的应用程序,并且主要在Release中工作,但是我在这个问题的底部得到了堆栈跟踪。似乎失败的绑定是按钮的enabled属性。我认为这是因为如果我把它拿出来其他绑定工作。同样简化了viewmodel代码和ui axml。我已将发布和调试版本之间的相关差异本地化为“使用共享运行时”#34;构建设置。选择此选项后即可使用。没有它失败。不幸的是,如果选择此选项,我无法打包构建以进行分发。有谁知道这里发生了什么?

VisualStudio 2012 Xamarin 3.5.58.0

用于导致问题的按钮的ui axml片段

<Button
   android:padding="2dip"
   android:layout_weight="1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   local:MvxBind="Click SendConfigCommand; Enabled ButtonEnabledSendConfig; Text ButtonLabelSendConfig, Mode=OneTime; LongClick HelpCommand, CommandParameter=HelpContinueDevice" />

ViewModel代码段,用于按钮使用的属性(简化为debuggingbuoi)

    public string ButtonLabelSendConfig
    {
        get{return "Next";}
    }

    public bool ButtonEnabledSendConfig
    {
        get { return false; }
    }

    public ICommand SendConfigCommand
    {
        get
        {
            return m_SendConfigCommand ?? (m_SendConfigCommand = new MvxCommand(() =>
            {
               //Intentionally left empty
            }));
        }
    }
    private MvxCommand m_SendConfigCommand;
来自viewmodel基类

    public MvxCommand<string> HelpCommand
    {
        get { return this._HelpCommand ?? (this._HelpCommand = new MvxCommand<string>(key =>
        {
            var prompter = Mvx.Resolve<IUserInteraction>();
            prompter.Alert(Localised.ResourceManager.GetString(key), null, Localised.Help, Localised.OkayButton);
        })); }
    }
    private MvxCommand<string> _HelpCommand;

堆栈跟踪

MvxBind:Error:653.39 Exception thrown during the view binding NullReferenceException: Object reference not set to an instance of an object
09-16 13:21:45.456 I/MvxBind (29040): 653.39 Exception thrown during the view binding NullReferenceException: Object reference not set to an instance of an object

      at Cirrious.CrossCore.ReflectionExtensions+<>c__DisplayClass7.<GetProperties>b__4 (System.Reflection.PropertyInfo p) [0x00000] in <filename unknown>:0 
  at System.Linq.Enumerable+<CreateWhereIterator>c__Iterator1F`1[System.Reflection.PropertyInfo].MoveNext () [0x00000] in <filename unknown>:0 
  at System.Linq.Enumerable+<CreateWhereIterator>c__Iterator1E`1[System.Reflection.PropertyInfo].MoveNext () [0x00000] in <filename unknown>:0 
  at System.Linq.Enumerable+<CreateWhereIterator>c__Iterator1E`1[System.Reflection.PropertyInfo].MoveNext () [0x00000] in <filename unknown>:0 
  at System.Linq.Enumerable.First[PropertyInfo] (IEnumerable`1 source, System.Func`2 predicate, Fallback fallback) [0x00000] in <filename unknown>:0 
  at System.Linq.Enumerable.FirstOrDefault[PropertyInfo] (IEnumerable`1 source, System.Func`2 predicate) [0x00000] in <filename unknown>:0 
  at Cirrious.CrossCore.ReflectionExtensions.GetProperty (System.Type type, System.String name) [0x00000] in <filename unknown>:0 
  at Cirrious.M
09-16 13:21:45.456 I/MvxBind (29040):     at Cirrious.CrossCore.ReflectionExtensions+<>c__DisplayClass7.<GetProperties>b__4 (System.Reflection.PropertyInfo p) [0x00000] in <filename unknown>:0 
[0:] MvxBind:Error:653.39 Exception thrown during the view binding NullReferenceException: Object reference not set to an instance of an object
      at Cirrious.CrossCore.ReflectionExtensions+<>c__DisplayClass7.<GetProperties>b__4 (System.Reflection.PropertyInfo p) [0x00000] in <filename unknown>:0 
  at System.Linq.Enumerable+<CreateWhereIterator>c__Iterator1F`1[System.Reflection.PropertyInfo].MoveNext () [0x00000] in <filename unknown>:0 
  at System.Linq.Enumerable+<CreateWhereIterator>c__Iterator1E`1[System.Reflection.PropertyInfo].MoveNext () [0x00000] in <filename unknown>:0 
  at System.Linq.Enumerable+<CreateWhereIterator>c__Iterator1E`1[System.Reflection.PropertyInfo].MoveNext () [0x00000] in <filename unknown>:0 
  at System.Linq.Enumerable.First[PropertyInfo] (IEnumerable`1 source, System.Func`2 predicate, Fallback fallback) [0x00000] in <filename unknown>:0 
  at System.Linq.Enumerable.FirstOrDefault[PropertyInfo] (IEnumerable`1 source, System.Func`2 predicate) [0x00000] in <filename unknown>:0 
  at Cirrious.CrossCore.ReflectionExtensions.GetProperty (System.Type type, System.String name) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Binding.Bindings.Target.Construction.MvxTargetBindingFactoryRegistry.TryCreateReflectionBasedBinding (System.Object target, System.String targetName, IMvxTargetBinding& binding) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Binding.Bindings.Target.Construction.MvxTargetBindingFactoryRegistry.CreateBinding (System.Object target, System.String targetName) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding.CreateTargetBinding (System.Object target) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding..ctor (Cirrious.MvvmCross.Binding.MvxBindingRequest bindingRequest) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Binding.Binders.MvxFromTextBinder.BindSingle (Cirrious.MvvmCross.Binding.MvxBindingRequest bindingRequest) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Binding.Binders.MvxFromTextBinder+<>c__DisplayClass1.<Bind>b__0 (Cirrious.MvvmCross.Binding.Bindings.MvxBindingDescription description) [0x00000] in <filename unknown>:0 
  at System.Linq.Enumerable+<CreateSelectIterator>c__Iterator10`2[Cirrious.MvvmCross.Binding.Bindings.MvxBindingDescription,Cirrious.MvvmCross.Binding.Bindings.IMvxUpdateableBinding].MoveNext () [0x00000] in <filename unknown>:0 
  at System.Collections.Generic.List`1[Cirrious.MvvmCross.Binding.Bindings.IMvxUpdateableBinding].AddEnumerable (IEnumerable`1 enumerable) [0x00000] in <filename unknown>:0 
  at System.Collections.Generic.List`1[Cirrious.MvvmCross.Binding.Bindings.IMvxUpdateableBinding].AddRange (IEnumerable`1 collection) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Binding.Droid.Binders.MvxAndroidViewBinder.ApplyBindingsFromAttribute (Android.Views.View view, Android.Content.Res.TypedArray typedArray, Int32 attributeId) [0x00000] in <filename unknown>:0 
vvmCross.Binding.Bindings.Target.Construction.MvxTargetBindingFactoryRegistry.TryCreateReflectionBasedBinding (System.Object target, System.String targetName, IMvxTargetBinding& binding) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Binding.Bindings.Target.Construction.MvxTargetBindingFactoryRegistry.CreateBinding (System.Object target, System.String targetName) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding.CreateTargetBinding (System.Object target) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding..ctor (Cirrious.MvvmCross.Binding.MvxBindingRequest bindingRequest) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Binding.Binders.MvxFromTextBinder.BindSingle (Cirrious.MvvmCross.Binding.MvxBindingRequest bindingRequest) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Binding.Binders.MvxFromTextBinder+<>c__DisplayClass1.<Bind>b__0 (Cirrious.MvvmCross.Binding.Bindings.MvxBindingDescription descr
09-16 13:21:45.456 I/MvxBind (29040):   at System.Linq.Enumerable+<CreateWhereIterator>c__Iterator1F`1[System.Reflection.PropertyInfo].MoveNext () [0x00000] in <filename unknown>:0 
iption) [0x00000] in <filename unknown>:0 
  at System.Linq.Enumerable+<CreateSelectIterator>c__Iterator10`2[Cirrious.MvvmCross.Binding.Bindings.MvxBindingDescription,Cirrious.MvvmCross.Binding.Bindings.IMvxUpdateableBinding].MoveNext () [0x00000] in <filename unknown>:0 
  at System.Collections.Generic.List`1[Cirrious.MvvmCross.Binding.Bindings.IMvxUpdateableBinding].AddEnumerable (IEnumerable`1 enumerable) [0x00000] in <filename unknown>:0 
  at System.Collections.Generic.List`1[Cirrious.MvvmCross.Binding.Bindings.IMvxUpdateableBinding].AddRange (IEnumerable`1 collection) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Binding.Droid.Binders.MvxAndroidViewBinder.ApplyBindingsFromAttribute (Android.Views.View view, Android.Content.Res.TypedArray typedArray, Int32 attributeId) [0x00000] in <filename unknown>:0 

1 个答案:

答案 0 :(得分:1)

这很可能是由于链接器造成的。 MvvmCross使用反射来执行数据绑定。由于 Enabled 属性的唯一引用位于布局文件中,因此链接器不会在最终程序集中包含该属性。

MvvmCross在您的Android项目中添加名为 LinkerPleaseInclude.cs 的文件。您需要在其中添加对该属性的引用,以确保它被链接。

这样的事情:

    public void Include(Button button)
    {
        button.Click += (s,e) => button.Text = button.Text + "";
        button.Enabled = !button.Enabled;
    }

另请参阅:mvvmcross binding on switch fails on release