即使仅在iOS中使用绑定,Xamarin.Forms + ReactiveUI Picker也不会从ViewModel更新为View

时间:2019-06-04 16:59:55

标签: c# xamarin xamarin.forms reactiveui rx.net

我正在使用Reactive UI将Xamarin.Forms Picker SelectedItem绑定到ViewModel属性。 (使用Bind(),两种方式绑定)。当我在ViewModel中设置“ Event”属性时(填充SourceCache列表后)应更新Picker中的选定项,但仅适用于Android,不适用于iOS。

我正在使用Reactive UI 9.16.6和Xamarin Forms 3.6.0.344457和Reactive.Fody

页面

namespace TicketPassManager.Forms.Pages.Dashboards.Invitations
{
    public class InvitationPage : ReactiveContentPage<InvitationPageViewModel>
    {
        Picker eventPicker;

        public InvitationPage()
        {
            eventPicker = new Picker
            {
                Title = "Evento",
                ItemDisplayBinding = new Binding(nameof(Event.Title)),
                TabIndex = 0
            };

            var stackLayout = new StackLayout
            {
                Children =
                {
                    eventPicker,
                }
            };

            Content = new ScrollView
            {
                Content = stackLayout
            };

                this.OneWayBind(ViewModel, vm => vm.EventList, v => v.eventPicker.ItemsSource).DisposeWith(disposables);
                this.Bind(ViewModel, vm => vm.Event, v => v.eventPicker.SelectedItem).DisposeWith(disposables);

                this.WhenAnyValue(v => v.ViewModel.LoadEventsCommand)
                    .Where(x => x != null)
                    .Select(x => Unit.Default)
                    .ObserveOn(RxApp.TaskpoolScheduler)
                    .InvokeCommand(ViewModel.LoadEventsCommand)
                    .DisposeWith(disposables);

                           });
        }
    }
}

视图模型

namespace TicketPassManager.Forms.ViewModels.Dashboards.Invitations
{
    public class InvitationPageViewModel : ReactiveObject
    { 

        [Reactive] public Event Event { get; set; }

        public ReactiveCommand<Unit, EventCollection> LoadEventsCommand;

        public ObservableCollectionExtended<Event> EventList;
        private SourceCache<Event, long> SourceCacheEventList;

        public InvitationPageViewModel()
        {
            EventList = new ObservableCollectionExtended<Event>();

            SourceCacheEventList = new SourceCache<Event, long>(key => key.Id);
                var canExecuteLoadEventCommand =
                this.WhenAnyObservable(x => x.SourceCacheEventList.CountChanged)
                    .StartWith(0)
                    .Select(x => x == 0);

            LoadEventsCommand = ReactiveCommand.CreateFromObservable<Unit, EventCollection>(ExecuteLoadEvents, canExecuteLoadEventCommand);

            SourceCacheEventList
                .Connect()
                .DeferUntilLoaded()
                .Sort(SortExpressionComparer<Event>.Ascending(t => t.StartDateTime))
                .ObserveOn(RxApp.MainThreadScheduler)
                .Bind(EventList)
                .DisposeMany()
                .Subscribe();

                this.WhenAnyValue(x => x.Event)
                .Where(x => x != null)
                .InvokeCommand(LoadTicketTypesCommand);


            LoadEventsCommand.Subscribe(x =>
            {
                if (x.Any())
                {
                    SourceCacheEventList.Clear();
                    SourceCacheEventList.AddOrUpdate(x.Where(e => e.IsUpcoming));
                    Event = SourceCacheEventList.Items.OrderBy(e => e.StartDateTime).First();
                }
            });

        }

        private IObservable<EventCollection> ExecuteLoadEvents(Unit arg)
        {
            return Resolver.Resolve<TicketPassService>().GetEventsFromLocalAsync().ToObservable();
        }

    }
}

0 个答案:

没有答案