【问题标题】:Xamarin.Forms + ReactiveUI MasterPage OnItemListenerXamarin.Forms + ReactiveUI MasterPage OnItemListener
【发布时间】:2017-12-19 02:58:49
【问题描述】:

如何在 MasterPage 后面的代码和 this.WhenActivated(disposables => {}); 函数中收听 MasterPage.OnItemSelected?或者我应该以某种方式在视图模型中执行此操作?

【问题讨论】:

    标签: xamarin.forms reactiveui


    【解决方案1】:

    您可以使用Observable.FromEventPattern。 假设您在 MasterPage 中使用 ListView:

    this.WhenActivated (d => {
        Observable.FromEventPattern (h => MasterPage.ListView.ItemSelected += h, 
                                     h => MasterPage.ListView.ItemSelected -= h)
                  .Subscribe (DoSomethingWithMySelectedItem)
                  .DisposeWith (d);
    });
    

    【讨论】:

      【解决方案2】:

      在 Qonstukt 的帮助下,我最终做到了:

      Observable.FromEventPattern<SelectedItemChangedEventArgs>(h => masterPage.ListView.ItemSelected += h,
                                                      h => masterPage.ListView.ItemSelected -= h)
                                  .Subscribe(x => OnItemSelected(x.Sender, x.EventArgs))
                                  .DisposeWith(disposables);
      

      ...

      void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
          {
              if (e.SelectedItem is MasterPageItem item)
              {
                  var nextPage = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));
                  nextPage.BarBackgroundColor = Constants.ToolbarColor;
                  nextPage.BarTextColor = Constants.ToolbarTextColor;
                  Detail = nextPage;
                  masterPage.ListView.SelectedItem = null;
                  IsPresented = false;
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2016-05-06
        • 1970-01-01
        • 2019-12-04
        • 2017-08-06
        • 2023-03-08
        • 1970-01-01
        • 1970-01-01
        • 2014-07-29
        • 1970-01-01
        相关资源
        最近更新 更多