android 的 MVVMCross - 如何在代码中进行绑定? [英] MVVMCross for android - how to do binding in code?
问题描述
我想使用 MVVMCross,但是对于我的 android 应用程序,我还想使用其他库(滑动菜单和操作栏),这些库要求我从他们的自定义类继承我的活动类.这阻止了我继承 MvxActivity,但我注意到在 iOS 的 MVVMCross 中,您可以在代码中完成所有绑定(请参阅 https://github.com/slodge/NPlus1DaysOfMvvmCross/blob/master/N-00-FirstDemo/FirstDemo.Touch/Views/FirstView.cs)
var set = this.CreateBindingSet();set.Bind(textEditFirst).To(vm => vm.FirstName);set.Bind(textEditSecond).To(vm => vm.LastName);set.Bind(labelFull).To(vm => vm.FullName);设置.应用();
Android 有没有办法做到这一点?
是的 - 如果需要,您可以在 Android 中使用流畅的绑定.
完全相同的代码应该可以工作.
您需要使用 FindViewById
获取对 ui 控件的引用,然后才能绑定它们.
例如,在 TipCalc 中,您可以添加已识别的控件,例如:
然后使用以下方法实现绑定:
protected override void OnViewModelSet(){SetContentView(Resource.Layout.View_Tip);var edit = this.FindViewById(Resource.Id.FluentEdit);var set = this.CreateBindingSet();set.Bind(edit).To(vm => vm.SubTotal);设置.应用();//对于非默认属性,使用 'For'://set.Bind(edit).For(ed => ed.Text).To(vm => vm.SubTotal);//你也可以使用://.WithConversion("converter", "可选参数")//.OneTime()、.OneWay() 或 .TwoWay()}
此外,您可以通过以下方式将任何 FooActivity 转换为数据绑定 MvxFooActivity:
- 从 FooActivity 继承以提供来自 EventSourceFooActivity 中的生命周期事件的事件
- 从 EventSourceFooActivity 继承以在 MvxFooActivity 中提供数据上下文
- 然后您可以在从 MvxFooActivity 继承的活动中编写代码
要查看所需的代码,请参阅:
- https://github.com/slodge/MvvmCross/blob/v3/Crillous/Cirrious.MvvmCross.Droid.Fragging/MvxEventSourceFragmentActivity.cs
- https://github.com/slodge/MvvmCross/blob/v3/Crillous/Cirrious.MvvmCross.Droid.Fragging/MvxFragmentActivity.cs
您将在所有 mvx 改编的活动中看到相同的代码 - MvxActivity、MvxTabActivity ......这里有一点剪切和粘贴,但尽可能多的代码放在共享扩展方法中.>
在以前的版本中,人们使用这种技术来绑定 monogame 和 google 广告活动 - 例如,请参阅 在 MvvmCross monodroid Activity 中插入 Monogame 视图
I want to use MVVMCross, however for my android application I also want to use other libraries (sliding menu and action bar) which require me to inherit my activity classes from their custom class. This prevents me from inheriting MvxActivity, but I noticed that in MVVMCross for iOS, you can do all your bindings in code (see https://github.com/slodge/NPlus1DaysOfMvvmCross/blob/master/N-00-FirstDemo/FirstDemo.Touch/Views/FirstView.cs)
var set = this.CreateBindingSet<FirstView, FirstViewModel>();
set.Bind(textEditFirst).To(vm => vm.FirstName);
set.Bind(textEditSecond).To(vm => vm.LastName);
set.Bind(labelFull).To(vm => vm.FullName);
set.Apply();
Is there any way to do that in Android?
Yes - you can use fluent bindings in Android if you want to.
Exactly the same code should work.
You'll need to get references to the ui controls using FindViewById<Type>()
, then you can bind them.
For example, in TipCalc you can add identified controls like:
<EditText
android:id="@+id/FluentEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:textSize="24dp"
android:gravity="right"
/>
and then implement binding using:
protected override void OnViewModelSet()
{
SetContentView(Resource.Layout.View_Tip);
var edit = this.FindViewById<EditText>(Resource.Id.FluentEdit);
var set = this.CreateBindingSet<TipView, TipViewModel>();
set.Bind(edit).To(vm => vm.SubTotal);
set.Apply();
// for non-default properties use 'For':
// set.Bind(edit).For(ed => ed.Text).To(vm => vm.SubTotal);
// you can also use:
// .WithConversion("converter", "optional parameter")
// .OneTime(), .OneWay() or .TwoWay()
}
Additionally, you can convert any FooActivity into a data-binding MvxFooActivity by:
- inheriting from FooActivity to provide events from lifetime events in an EventSourceFooActivity
- inheriting from EventSourceFooActivity to provide a datacontext in an MvxFooActivity
- you can then write your code inside activities inheriting from MvxFooActivity
To see, the code required, see:
- https://github.com/slodge/MvvmCross/blob/v3/Cirrious/Cirrious.MvvmCross.Droid.Fragging/MvxEventSourceFragmentActivity.cs
- https://github.com/slodge/MvvmCross/blob/v3/Cirrious/Cirrious.MvvmCross.Droid.Fragging/MvxFragmentActivity.cs
You'll see the same code in all the mvx adapted Activities - MvxActivity, MvxTabActivity, ... There is a little cut-and-paste here, but as much code as possible is place in shared extension methods.
In previous versions, people have used this technique to bind monogame and google ads activities - eg see Insert a Monogame view inside MvvmCross monodroid Activity
这篇关于android 的 MVVMCross - 如何在代码中进行绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!