【问题标题】:Can't bind a button in a listview to ViewModel using ReactiveUI无法使用 ReactiveUI 将列表视图中的按钮绑定到 ViewModel
【发布时间】:2018-08-16 14:14:52
【问题描述】:

我正在使用 ReactiveUI 将按钮绑定到 ReactiveCommand 但它没有响应它,所以我在这里错过了什么??

这是 LikeCommand

     LikesCommand = ReactiveCommand.Create<Button>(LikeChanges);


private void LikeChanges(Button sender)
    {
        sender.Clicked += (s, e) =>
        {
            _newsFeed.LikesNum++;
            _newsFeed.BackgroundColor = Color.White;
            _newsFeed.TextColor = Color.DodgerBlue;
            RaisePropertyChanged();
        };
    }

这是删除命令

 DeleteCommand = ReactiveCommand.Create<NewsFeed>(DeleteItem);

     public async void DeleteItem(NewsFeed news)
    {
        var res = await CoreMethods.DisplayAlert("OH !", "Are you sure?", 
                 "Yes", "No");
        if (res)
        {
            NewsFeeds.Remove(news);
            RaisePropertyChanged();
        }

    }

这是我在列表视图中 viewCell 中的按钮

     <Button Text="{Binding LikesNum , StringFormat='Like {0}'}"
           x:Name="btnLike"
           Command="{Binding LikesCommand}"
           BackgroundColor="{Binding BackgroundColor}"
           TextColor="{Binding TextColor}"
           HorizontalOptions="StartAndExpand" />
      <Button Text="Delete"
            x:Name="btnLike"
           Clicked="Button_OnClicked"
           Command="{Binding DeleteCommand}"
           HorizontalOptions="End" />                          

【问题讨论】:

  • 首先,删除 LikeChanges 中的 sender.Clicked 委托。 ReactiveCommand 负责处理 clicked 事件。您只需要该 lambda 中的内容。让你的 ReactiveCommand 签名看起来像 ReactiveCommand LikesCommand

标签: xaml xamarin.forms reactiveui


【解决方案1】:

几个问题。

Create 上的泛型用于命令参数,但您尚未定义。通常,您会将所选元素绑定到视图模型。

创建意愿是当您执行以下操作时:

<Button Command="{Binding DeleteCommand, Mode=OneWay}" CommandParameter="{Binding ElementName=ListBox, Name=SelectedItem}" />

Create 中的 T 与您通过 CommandParameter 传入的类型相匹配。另一种方法是在 ViewModel 上拥有一个名为 SelectedItem 的属性,然后将 SelectedItem 绑定到该属性。

您可能想要 {Binding DeleteCommand, Mode=OneWay}

避免返回 async void,改为使用 public async Task 并使用 CreateFromTask 重载。

您的按钮上有一个单击和一个命令。你想要一个或另一个。

另请参阅 https://reactiveui.net/docs/handbook/commands/ 了解更多想法。

还可以考虑使用响应式 UI 绑定。

【讨论】:

  • 我也刚刚推送了一些更新的文档,这些文档刚刚经历了很多这样的东西。给它 15 分钟,命令文档将会更新。
  • sry 但我是 ReactiveUI 的新手,你能解释一下吗?我研究了文档中的命令..但是关于reactiveUI绑定,我尝试以这种方式绑定它:this.BindCommand(new NewsFeedListPageModel(_databaseService), vm => vm.LikesCommand, ),这是错误的。它没有将视图作为 lambda 第三个参数。你能为我写正确的方法吗?对于泛型,我在命令中传递了按钮的参数,所以这里有什么问题?谢谢
  • 再次查看该页面。它实际上在最后一分钟左右发生了变化,涵盖了您遇到的许多问题。还编辑了更多信息。
  • 嗯也值得注意,你的 LikeChanges 很可能是错误的。命令概念取代了点击。所以在 LikeChanges 方法中不要有事件处理程序,也不要为传递按钮而烦恼。
  • 公共覆盖 void Init(object initData) { base.Init(initData); NewsFeeds = new ObservableCollection(_databaseService.GetNewsFeeds()); AddNewsFeedCommand = ReactiveCommand.CreateFromTask(async () => await CoreMethods.PushPageModel()); SelectedItemCommand = ReactiveCommand.Create(SelectedItemAction); LikesCommand = ReactiveCommand.Create
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 2012-04-21
相关资源
最近更新 更多