使用ReactiveUI

时间:2018-05-21 07:52:50

标签: data-binding xamarin.android android-spinner reactiveui

我已经通过以下参考 Xamarin.Android and Spinner binding with ReactiveUI 并尝试按如下方式绑定微调器。

这是我的ViewModel类,

public class ExampleVM : ReactiveObject{
    ReactiveList<ExampleTypeResult> _list;
            public ReactiveList<ExampleTypeResult> ExampleList
            {
                get { return _list; }
                private set { this.RaiseAndSetIfChanged(ref _list, value); }
            }
    } 

这是My Fragment类,

public class ExampleFragment : ExampleBaseFragment<ExampleVM>{
private ReactiveList<ExampleTypeResult> ExampleList;
            Action<ReactiveList<ExampleTypeResult>> action =
                list =>
                {
                    var mItems = ExampleList.Select(n => n._Name).ToList();
                    var adater = new ArrayAdapter<string>(this.Activity, Android.Resource.Layout.SimpleSpinnerItem, mItems);
                    adater.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
                    FragmentExampleSpinner.Adapter = adater;
                };

         this.Bind(this.ViewModel, vm => vm.ExampleList, v => v.ExampleList, action)
        .DisposeWith(SubscriptionDisposables);
}

但这甚至没有错误。我想知道如何将反应列表正确绑定到Android.Widget.Spinner?

1 个答案:

答案 0 :(得分:0)

您可以尝试在ViewModel中创建ArrayAdapter属性。然后使用此arrayadapter绑定微调器 例如:

public class TheViewModel : ReactiveObject
{
    private ArrayAdapter theadapter;
    public ArrayAdapter Theadapter
    {
        get { return this.theadapter; }
        set { this.RaiseAndSetIfChanged(ref this.theadapter, value); }
    }
}

并将适配器设置为活动:

        var mItems = new ReactiveList<string>();
        mItems.Add("AAA");
        mItems.Add("BBB");
        mItems.Add("CCC");
        mItems.Add("DDD");
        mItems.Add("EEE");
        mItems.Add("FFF");
        ViewModel.Theadapter = new ArrayAdapter(this, Resource.Layout.SimpleSpinnerItem, mItems);
        this.OneWayBind(this.ViewModel, vm => vm.Theadapter, v => v.TheSpinner.Adapter);