我正在尝试在Xamarin.Android应用程序中使用ReactiveUI绑定到Spinner。要将项目添加到 Spinner ,我需要使用 ArrayAdapter 。但是 ArrayAdapter 需要 Android.Content.Context 。我应该将其传递给ViewModel吗?
I'm trying to bind to a Spinner using ReactiveUI in a Xamarin.Android application. To add items to the Spinner, I need to use ArrayAdapter. But ArrayAdapter needs Android.Content.Context. Should I pass it into the ViewModel?
有人知道Xamarin.Android编写的应用程序,该应用程序使用ReactiveUI,在哪里可以找到灵感? ReactiveUI文档仅引用了为iOS编写的示例应用程序。
Anyone know about an application written in Xamarin.Android, which uses ReactiveUI, where I could look inspiration? The ReactiveUI documentation only has a reference to a sample application written for iOS.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="schemas.android/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/Label" android:text="Zařízení:" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Spinner android:id="@+id/Devices" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/Label"/> <Button android:id="@+id/EditCommand" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/Devices" android:text="Edit"/> </RelativeLayout>活动
Activity
namespace Test.Droid { [Activity(Label = "Test.Droid", MainLauncher = true)] public class MainActivity : ReactiveActivity, IViewFor<MainViewModel> { public Spinner Devices { get; private set; } protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.Main); ViewModel = new MainViewModel(); this.WireUpControls(); // Bindings this.Bind(this.ViewModel, ) // ? } private MainViewModel _viewModel; public MainViewModel ViewModel { get => _viewModel; set { this.RaiseAndSetIfChanged(ref _viewModel, value); } } object IViewFor.ViewModel { get => ViewModel; set { ViewModel = (MainViewModel)value; } } } }ViewModel
ViewModel
namespace Test.Droid.ViewModels { public class MainViewModel : ReactiveObject { // How to databind Spinner Devices ? public MainViewModel() { } } }推荐答案
我没有做过Xamarin.Android开发,但总的来说,您不想传递有关视图的详细信息
I haven't done any Xamarin.Android development, but in general you don't want to pass details about the view into the ViewModel - it should not know anything about the view.
我会将项目列表作为集合公开(例如, IList< Item> ),并在绑定上使用转换器创建 ArrayAdapter :
I would expose the list of items as a collection (e.g. IList<Item>) and use a converter on the binding to create an ArrayAdapter:
this.OneWayBind(this.ViewModel.Devices, this.View.Property, devices => new ArrayAdapter(devices));this.View.Property 应该引用当设备列表更改时更改的属性。第三个参数( devices => new ArrayAdapter())从ViewModel接收属性作为参数,然后返回可以在 this.View.Property 。
this.View.Property should refer to the property that changes whenever the list of devices changes. The third parameter (devices => new ArrayAdapter()) receives the property from the ViewModel as an argument, you then return a value that can be set on the this.View.Property.
例如:
- ViewModel.Count 是字符串
- View.Property 是 int
- ViewModel.Count is a string
- View.Property is an int
这样的绑定:
this.OneWayBind(this.ViewModel.Count, this.View.Property, count => int.Parse(count));
第三个参数可以是接受参数的函数或lambda的ViewModel属性的类型,并返回该视图属性的类型的值。
The third parameter can be a function or lambda that accepts an argument of the type of the ViewModel property and returns a value of the type of the view property.
更多推荐
Xamarin.Android和Spinner与ReactiveUI绑定
发布评论