我正在使用 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" />
情侣问题。
Create 上的泛型用于命令参数,但您尚未定义。通常,您会将所选元素绑定到视图模型。
创建将是当您执行以下操作时:
<Button Command="{Binding DeleteCommand, Mode=OneWay}" CommandParameter="{Binding ElementName=ListBox, Name=SelectedItem}" />
Create 中的 T 与您通过 CommandParameter 传入的类型匹配。另一种方法是在您的 ViewModel 上在您的 ViewModel 上拥有一个名为 SelectedItem 的属性,然后将 SelectedItem 绑定到该属性。
你可能想要 {Binding DeleteCommand, Mode=OneWay}
避免返回async void,改为执行public async Task并改用 CreateFromTask 重载。
您在按钮上单击了一个命令。你想要一个或另一个。
另请参阅https://reactiveui.net/docs/handbook/commands/了解更多想法。
还可以考虑使用reactiveui 绑定。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句