问题描述
我想使用MVVMCross,但是对于我的android应用程序,我也想使用其他库(滑动菜单和操作栏),这需要我从其自定义类继承我的活动类.这阻止了我继承MvxActivity,但是我注意到在MVVMCross for iOS中,您可以在代码中完成所有绑定(请参见))
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();
在Android中有什么方法可以做到吗?
Is there any way to do that in Android?
推荐答案
是的-如果需要,您可以在Android中使用流畅的绑定.
Yes - you can use fluent bindings in Android if you want to.
完全相同的代码应该起作用.
Exactly the same code should work.
您需要使用FindViewById<Type>()
获取对ui控件的引用,然后才能绑定它们.
You'll need to get references to the ui controls using FindViewById<Type>()
, then you can bind them.
例如,在TipCalc中,您可以添加已识别的控件,例如:
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()
}
此外,您还可以通过以下方式将任何FooActivity转换为数据绑定的MvxFooActivity:
Additionally, you can convert any FooActivity into a data-binding MvxFooActivity by:
- 继承自FooActivity,以提供EventSourceFooActivity中生命周期事件的事件
- 继承自EventSourceFooActivity以在MvxFooActivity中提供数据上下文
- 然后您可以在继承自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
- 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
在所有经过mvx改编的活动中,您将看到相同的代码-MvxActivity,MvxTabActivity,...这里有一些剪切和粘贴操作,但是在共享扩展方法中放置了尽可能多的代码.
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.
在以前的版本中,人们使用这种技术来绑定monogame和google ads活动-例如,请参见
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
这篇关于MVVMCross for Android-如何在代码中进行绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!