C# MvvmCross:传递给MvxTargetBindingFactoryRegistry的空绑定目标
我用Xamarin和MvvmCross创建了一个Android应用程序。 我想将一些视图(文本、编辑文本、按钮)绑定到我的ViewModel。到目前为止没有什么奇怪的。但是我的绑定不适用……当我使用类型化FindViewById时,我不会得到跟踪错误,但绑定不适用 运行应用程序时,我有以下跟踪:C# MvvmCross:传递给MvxTargetBindingFactoryRegistry的空绑定目标,c#,android,xamarin,mvvmcross,C#,Android,Xamarin,Mvvmcross,我用Xamarin和MvvmCross创建了一个Android应用程序。 我想将一些视图(文本、编辑文本、按钮)绑定到我的ViewModel。到目前为止没有什么奇怪的。但是我的绑定不适用……当我使用类型化FindViewById时,我不会得到跟踪错误,但绑定不适用 运行应用程序时,我有以下跟踪: MvxBind:Error: Empty binding target passed to MvxTargetBindingFactoryRegistry MvxBind:Warning: Failed
MvxBind:Error: Empty binding target passed to MvxTargetBindingFactoryRegistry
MvxBind:Warning: Failed to create target binding for binding for TextProperty
我的OnCreate(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);
我拥有与原始类相同的LinkerPleaseInclude
类。
我的设置继承自MvxAndroidSetup
class
在其他ViewModels上,绑定应用正确。我认为您不需要绑定两次,请删除以下行:
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();
并将绑定保留在axml文件中
确保在xaml文件的顶部有以下内容:
xmlns:local=”http://schemas.android.com/apk/res-auto“
另外,如果您在cs文件中进行绑定,MvvmCross绑定模式默认为双向。所以你不需要
.Mode(MvxBindingMode.TwoWay)代码>我认为您不需要绑定两次,请删除以下行:
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();
并将绑定保留在axml文件中
确保在xaml文件的顶部有以下内容:
xmlns:local=”http://schemas.android.com/apk/res-auto“
另外,如果您在cs文件中进行绑定,MvvmCross绑定模式默认为双向。所以你不需要.Mode(MvxBindingMode.TwoWay)代码>警告
MvxBind:错误:2,20个空绑定目标传递给MvxTargetBindingFactoryRegistry
MvxBind:警告:2,20未能为文本绑定创建目标绑定
是由var referenceTextView=FindViewById(Resource.Id.referenceEditView)引起的代码>导致referenceTextView
的类型为View
MvvmCross在为(targetProperty)
调用Bind
时正在搜索类型为TTarget
的默认绑定目标属性。这只是在如下表中查找:
TTarget Property
----------------------
TextView Text
Button Click
... ...
在您的情况下,TTarget
是View
而不是TextView
,因为您将它传递到bindingsSet.Bind(referenceTextView)
中,这是bindings.Bind(btnNumber)
的隐式调用<代码>视图
没有默认的绑定目标属性。您必须将其显式设置为
bindings.Bind(btnNumber).For("Text")
或者使用键入的FindViewById
警告
MvxBind:错误:2,20个空绑定目标传递给MvxTargetBindingFactoryRegistry
MvxBind:警告:2,20未能为文本绑定创建目标绑定
是由var referenceTextView=FindViewById(Resource.Id.referenceEditView)引起的代码>导致referenceTextView
的类型为View
MvvmCross在为(targetProperty)
调用Bind
时正在搜索类型为TTarget
的默认绑定目标属性。这只是在如下表中查找:
TTarget Property
----------------------
TextView Text
Button Click
... ...
在您的情况下,TTarget
是View
而不是TextView
,因为您将它传递到bindingsSet.Bind(referenceTextView)
中,这是bindings.Bind(btnNumber)
的隐式调用<代码>视图
没有默认的绑定目标属性。您必须将其显式设置为
bindings.Bind(btnNumber).For("Text")
或者使用键入的
FindViewById
调用base.OnCreate(bundle)
在SetContentView
之前,因为ViewModel位于并附加在该调用中。如果不这样做,显然会给您带来您所看到的确切错误。源将为null,并且不会绑定到目标
因此,您可以:
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(bundle);
SetContentView(Resource.Layout.Reference);
var referenceTextView=findviewbyd(Resource.Id.referenceEditView);
var sitextview=findviewbyd(Resource.Id.sitextview);
var bset=this.CreateBindingSet();
Bind(referenceTextView).To(vm=>vm.Reference);
Bind(siteTextView).To(vm=>vm.Site);
b设置应用();
首先确保调用
base.OnCreate
。您需要调用base.OnCreate(bundle)
在SetContentView
之前,因为ViewModel位于并附加在该调用中。如果不这样做,显然会给您带来您所看到的确切错误。源将为null,并且不会绑定到目标
因此,您可以:
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(bundle);
SetContentView(Resource.Layout.Reference);
var referenceTextView=findviewbyd(Resource.Id.referenceEditView);
var sitextview=findviewbyd(Resource.Id.sitextview);
var bset=this.CreateBindingSet();
Bind(referenceTextView).To(vm=>vm.Reference);
Bind(siteTextView).To(vm=>vm.Site);
b设置应用();
只要确保调用
base。一开始创建。当然,我可能说错了:我使用了我知道的不同方法。我有您在我的AXML文件中指定的行。啊,不用担心,通常在Android上,我倾向于在xaml中进行绑定,除非是不寻常的事情。当然,我可能说错了:我使用了我知道的不同方法。我有你在我的AXML文件中指定的行。啊,不用担心,通常在Android上,我倾向于在xaml中进行绑定,除非是不寻常的事情。我可能说错了。我使用了我知道的不同方法:我使用了类型化和非类型化的FindViewById方法。然后,按照Cheesebaron所说的,将base.OnCreate(bundle)
移动到函数的顶部(在调用它之前先调用它)。我可能有错误。我用了不同的方法,我知道:我用打字