我用Xamarin和MvvmCross创建了一个Android应用程序。我想将一些视图(文本、编辑文本、按钮)绑定到我的ViewModel上。到目前为止没有什么奇怪的事情。但是我的绑定不适用于…当我使用类型化的FindViewById时,我不会得到跟踪的错误,但是绑定不适用。
当我运行应用程序时,我有以下跟踪:
MvxBind:Error: Empty binding target passed to MvxTargetBindingFactoryRegistry
MvxBind:Warning: Failed to create target binding for binding for TextProperty我对OnCreate(Bundle bundle) 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类继承的,在其他ViewModels上,绑定是正确应用的。
发布于 2016-05-01 13:25:48
您需要在base.OnCreate(bundle);之前调用SetContentView,因为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。
发布于 2016-04-30 14:55:11
警告
MvxBind:错误:传递给MvxTargetBindingFactoryRegistry的2,20个空绑定目标:警告: 2,20未能为文本绑定创建目标绑定
都是由var referenceTextView = FindViewById(Resource.Id.referenceEditView);引起的,导致referenceTextView为View类型。
MvvmCross在调用没有 For(targetProperty)的Bind<TTArget> 时,正在搜索类型为TTarget的默认绑定目标属性。这只是在一张桌子上抬头看一下,就像:
TTarget Property
----------------------
TextView Text
Button Click
... ... 在您的示例中,TTarget是View而不是TextView,因为将它传递给bindingsSet.Bind(referenceTextView)是bindings.Bind<View>(btnNumber)的隐式调用。View没有默认绑定目标属性。您必须将其显式设置为
bindings.Bind(btnNumber).For("Text")或者使用键入的FindViewById<TextView>。
发布于 2016-04-30 14:13:23
我不认为你需要绑定两次,删除以下几行:
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);
https://stackoverflow.com/questions/36955783
复制相似问题