Xamarin.Forms ChangeCanExecute不使用正确的参数
我有一个运行在UWP中的Xamarin.Forms应用程序,我使用DLToolkit.Forms.Controls.FlowListView(从daniel-luberda)显示某种带按钮的网格视图。Xamarin.Forms ChangeCanExecute不使用正确的参数
使用的Mvvm框架是FreshMvvm。
这是我在XAML GridView控件:
<c:FlowListView SeparatorVisibility="None"
HasUnevenRows="False" FlowColumnMinWidth="100"
FlowItemsSource="{Binding Boards}">
<c:FlowListView.FlowColumnTemplate>
<DataTemplate>
<Button Text="{Binding Number}"
Command="{Binding Path=BindingContext.SelectBoardCommand, Source={x:Reference Name=SalesPage}}"
CommandParameter="{Binding .}" />
</DataTemplate>
</c:FlowListView.FlowColumnTemplate>
</c:FlowListView>
下面是结果(我简化关于颜色和大小的XAML):
我的GridView控件内的按钮被绑定到带参数的命令。每次我点击一个按钮,我想禁用它。
我的命令如下:
private ICommand _selectBoardCommand;
[DoNotNotify]
public ICommand SelectBoardCommand => _selectBoardCommand = new Command<BoardModel>(board =>
{
board.NotUsed = false;
((Command<BoardModel>) _selectBoardCommand).ChangeCanExecute();
}, board => board != null && board.NotUsed);
推动上的按钮调用与正确的参数命令(绑定到命令的文本board.Number是一个我在命令中获得) 。 但是,当为了禁用该命令而调用“CanChangeExecute”时,我无法将选定的板作为参数传递。
,并在命令,使用
((Command<BoardModel>) _selectBoardCommand).ChangeCanExecute();
时,它调用的函数功能
board => board != null && board.NotUsed
与列表的最新项目。
此外,我需要在ChangeCanExecute中放置一个“board!= null”,因为这个Func用空值调用了很多时间。
有什么想法?
我看到你的问题的两种方式:
1)移动SelectBoardCommand
里面BoardModel
。它不需要参数,并且可以单独在每个模型上运行;
2)绑定Enabled
财产Button
到NotUsed
财产。在这种情况下,用户将无法从UI调用SelectBoardCommand
。
我无法在BoardModel中输入命令,因为在与ViewModel紧密结合的命令中有更多的代码。 – hugoterelle
我已经尝试绑定到按钮的Enabled属性,但只要您使用Command绑定按钮,Enabled就不再使用。 – hugoterelle
如果我们记住,在技术上我们使用'ListView'和'ItemTappedCommand',该命令不应该'CanExecute'返回'false'。单元总是可以被点击,但是在'command'实现中,我们把'if(board?.NotUsed){...}' – foxanna
你确定它不是FlowListView的问题吗? – Andy
@Andy刚刚测试过,并且出现同样的问题 – hugoterelle