首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MvvmCross :传递给MvxTargetBindingFactoryRegistry的空绑定目标

MvvmCross :传递给MvxTargetBindingFactoryRegistry的空绑定目标
EN

Stack Overflow用户
提问于 2016-04-30 13:59:35
回答 3查看 597关注 0票数 0

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

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

代码语言:javascript
复制
MvxBind:Error: Empty binding target passed to MvxTargetBindingFactoryRegistry
MvxBind:Warning: Failed to create target binding for binding for TextProperty

我对OnCreate(Bundle bundle) void的重写是:

代码语言:javascript
复制
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中尝试过这样做:

代码语言:javascript
复制
<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方法:

代码语言:javascript
复制
private string _reference;
public string Reference
{
    get { return _reference; }
    set { this.RaiseAndSetIfChanged(ref _reference, value, () => Reference); }
}

我的LinkerPleaseInclude类与LinkerPleaseInclude原始类相同。我的设置是从MvxAndroidSetup类继承的,在其他ViewModels上,绑定是正确应用的。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-05-01 13:25:48

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

所以你可以这样做:

代码语言:javascript
复制
base.OnCreate(bundle);
SetContentView(Resource.Layout.Reference);

并将所有绑定都放在AXML中。或者您可以执行另一种方法,在幕后设置绑定:

代码语言:javascript
复制
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

票数 1
EN

Stack Overflow用户

发布于 2016-04-30 14:55:11

警告

MvxBind:错误:传递给MvxTargetBindingFactoryRegistry的2,20个空绑定目标:警告: 2,20未能为文本绑定创建目标绑定

都是由var referenceTextView = FindViewById(Resource.Id.referenceEditView);引起的,导致referenceTextViewView类型。

MvvmCross在调用没有 For(targetProperty)Bind<TTArget> 时,正在搜索类型为TTarget的默认绑定目标属性。这只是在一张桌子上抬头看一下,就像:

代码语言:javascript
复制
TTarget       Property
----------------------
TextView      Text
Button        Click
...           ...  

在您的示例中,TTargetView而不是TextView,因为将它传递给bindingsSet.Bind(referenceTextView)bindings.Bind<View>(btnNumber)的隐式调用。View没有默认绑定目标属性。您必须将其显式设置为

代码语言:javascript
复制
bindings.Bind(btnNumber).For("Text")

或者使用键入的FindViewById<TextView>

票数 1
EN

Stack Overflow用户

发布于 2016-04-30 14:13:23

我不认为你需要绑定两次,删除以下几行:

代码语言:javascript
复制
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();

所以你的创作就是这样:

代码语言:javascript
复制
SetContentView(Resource.Layout.Reference);
base.OnCreate(bundle);

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

确保xaml文件顶部有以下内容:

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36955783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档