Xamarin.Android和Spinner与ReactiveUI绑定

编程入门 行业动态 更新时间:2024-10-28 12:30:15
本文介绍了Xamarin.Android和Spinner与ReactiveUI绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在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绑定

本文发布于:2023-11-12 18:04:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582168.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   Android   Xamarin   ReactiveUI   Spinner

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!