【问题标题】:MvvmCross : Empty binding target passed to MvxTargetBindingFactoryRegistryMvvmCross : 传递给 MvxTargetBindingFactoryRegistry 的空绑定目标
【发布时间】:2016-04-30 13:59:35
【问题描述】:

我使用 Xamarin 和 MvvmCross 创建了一个 Android 应用程序。 我想将一些视图(文本、编辑文本、按钮)绑定到我的 ViewModel。到目前为止没有什么奇怪的。但是我的绑定不适用……当我使用键入的 FindViewById 时,我没有得到跟踪的错误,但绑定不适用。

当我运行应用程序时,我有以下跟踪:

MvxBind:Error: Empty binding target passed to MvxTargetBindingFactoryRegistry
MvxBind:Warning: Failed to create target binding for binding for TextProperty

我对@9​​87654323@ void 的覆盖是:

SetContentView(Resource.Layout.Reference);
var referenceTextView = FindViewById(Resource.Id.referenceEditView); // untyped FindViewById
var siteTextView = FindViewById<TextView>(Resource.Id.siteTextView); // typed FindViewById<T>
//var goButton = FindViewById<Button>(Resource.Id.goButton);
var bindingsSet = this.CreateBindingSet<ReferenceView, ReferenceViewModel>();
bindingsSet.Bind(referenceTextView).To(vm => vm.Reference).Mode(MvxBindingMode.TwoWay);
bindingsSet.Bind(siteTextView).To(vm => vm.Site);
//bindingsSet.Bind(goButton).To(vm => vm.GoCommand);
bindingsSet.Apply();
base.OnCreate(bundle);

我已经尝试在 AXML 中做:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/siteTextView"
    android:text="####"
    local:MvxBind="Text Site"
    android:gravity="center" />
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/referenceTextView"
    android:hint="Numéro de dossier"
    local:MvxBind="Text Reference" />
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Accéder"
    android:id="@+id/goButton"
    local:MvxBind="Click GoCommand" />

我的属性的 getter 和 setter 使用 RaiseAndSetIfChanged 方法:

private string _reference;
public string Reference
{
    get { return _reference; }
    set { this.RaiseAndSetIfChanged(ref _reference, value, () => Reference); }
}

我的 LinkerPleaseInclude 课程与 LinkerPleaseInclude 原始课程相同。 我的设置继承自 MvxAndroidSetup 类 在其他 ViewModel 上,绑定应用正确。

【问题讨论】:

    标签: c# android xamarin mvvmcross


    【解决方案1】:

    您需要在 SetContentView 之前调用 base.OnCreate(bundle);,因为 ViewModel 位于并附加在该调用中。不这样做显然会给你你看到的确切错误。源将为空,不会绑定到目标。

    所以你可以这样做:

    base.OnCreate(bundle);
    SetContentView(Resource.Layout.Reference);
    

    并在 AXML 中包含所有绑定。或者您可以使用其他方法在幕后设置绑定:

    base.OnCreate(bundle);
    SetContentView(Resource.Layout.Reference);
    
    var referenceTextView = FindViewById<TextView>(Resource.Id.referenceEditView);
    var siteTextView = FindViewById<TextView>(Resource.Id.siteTextView);
    
    var bset = this.CreateBindingSet<ReferenceView, ReferenceViewModel>();
    bset.Bind(referenceTextView).To(vm => vm.Reference);
    bset.Bind(siteTextView).To(vm => vm.Site);
    bset.Apply();
    

    请务必先致电base.OnCreate

    【讨论】:

      【解决方案2】:

      警告

      MvxBind:Error: 2,20 Empty binding target 传递给 MvxTargetBindingFactoryRegistry MvxBind:Warning: 2,20 无法为文本绑定创建目标绑定

      var referenceTextView = FindViewById(Resource.Id.referenceEditView); 引起,导致referenceTextView 的类型为View

      MvvmCross 在调用Bind&lt;TTArget&gt; 没有 For(targetProperty) 时正在搜索TTarget 类型的默认绑定目标 属性。这只是在如下表中查找:

      TTarget       Property
      ----------------------
      TextView      Text
      Button        Click
      ...           ...  
      

      在您的情况下,TTargetView 而不是 TextView,因为您将它传递给 bindingsSet.Bind(referenceTextView),这是 bindings.Bind&lt;View&gt;(btnNumber) 的隐式调用。 View 没有默认绑定目标属性。你必须像这样明确地设置它

      bindings.Bind(btnNumber).For("Text")
      

      或使用输入的FindViewById&lt;TextView&gt;

      【讨论】:

      • 我可能说错了。我使用了我知道的不同方法:我使用了有类型和无类型的 FindViewById 方法。
      • 好吧,就像 Cheesebaron 所说的那样,将 base.OnCreate(bundle) 移动到函数的顶部(在其他所有内容之前调用它)。
      【解决方案3】:

      我认为你不需要绑定两次,删除这些行:

      var referenceTextView = FindViewById(Resource.Id.referenceEditView); // untyped FindViewById
      var siteTextView = FindViewById<TextView>(Resource.Id.siteTextView); // typed FindViewById<T>
      //var goButton = FindViewById<Button>(Resource.Id.goButton);
      var bindingsSet = this.CreateBindingSet<ReferenceView, ReferenceViewModel>();
      bindingsSet.Bind(referenceTextView).To(vm => vm.Reference).Mode(MvxBindingMode.TwoWay);
      bindingsSet.Bind(siteTextView).To(vm => vm.Site);
      //bindingsSet.Bind(goButton).To(vm => vm.GoCommand);
      bindingsSet.Apply();
      

      所以你的创作就是这样:

      SetContentView(Resource.Layout.Reference);
      base.OnCreate(bundle);
      

      并将绑定保存在 axml 文件中。

      确保您的 xaml 文件顶部有此内容:

      xmlns:local="http://schemas.android.com/apk/res-auto"

      另外如果你在cs文件中做绑定,MvvmCross绑定模式默认是TwoWay。所以你不需要.Mode(MvxBindingMode.TwoWay);

      【讨论】:

      • 当然,我可能听错了:我使用了我知道的不同方法。我的 AXML 文件中有您指定的行。
      • 啊,不用担心,通常在 Android 上,我倾向于在 xaml 中进行绑定,除非有异常情况。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      • 2017-07-18
      • 2013-10-17
      • 2014-10-10
      • 1970-01-01
      相关资源
      最近更新 更多