简体   繁体   English

Xamarin.Forms ChangeCanExecute没有使用正确的参数

[英]Xamarin.Forms ChangeCanExecute not using the right parameters

I have a Xamarin.Forms application running in UWP where I use the DLToolkit.Forms.Controls.FlowListView (from daniel-luberda ) to display some kind of grid view with buttons. 我有一个在UWP中运行的Xamarin.Forms应用程序,其中使用DLToolkit.Forms.Controls.FlowListView(来自daniel-luberda )来显示带有按钮的某种网格视图。

The Mvvm framework used is FreshMvvm. 使用的Mvvm框架是FreshMvvm。

Here is my gridview in XAML: 这是我在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>

Here is the result (I simplified the XAML concerning color and size) : 结果如下(我简化了有关颜色和大小的XAML):

网格视图

The button inside my gridview is binded to a command with a parameter. 我的gridview中的按钮绑定到带有参数的命令。 Each time I click on a button, I would like to disable it. 每次单击按钮时,我都想禁用它。

And my command looks like this: 我的命令如下所示:

    private ICommand _selectBoardCommand;

    [DoNotNotify]
    public ICommand SelectBoardCommand => _selectBoardCommand = new Command<BoardModel>(board =>
    {
        board.NotUsed = false;
        ((Command<BoardModel>) _selectBoardCommand).ChangeCanExecute();
    }, board => board != null && board.NotUsed);

Pushing on a button calls the command with the right parameter (the board.Number binded to the text of the command is the one I get in the command). 按下按钮会调用带有正确参数的命令(电路板。绑定到命令文本的编号是我在命令中获得的编号)。 But when calling the "CanChangeExecute" in order to disable the command, I can't pass the selected board as argument. 但是,当调用“ CanChangeExecute”以禁用命令时,我无法将选定的板作为参数传递。

And in the command, when using 在命令中,当使用

((Command<BoardModel>) _selectBoardCommand).ChangeCanExecute();

it calls the Func 它称为功能

board => board != null && board.NotUsed

with the latest item of the list. 与列表中的最新项。

Moreover, I needed to put a "board != null" in the ChangeCanExecute because this Func is called a lot of time with null values. 此外,我需要在ChangeCanExecute中放置“ board!= null”,因为很多时候此Func都使用null值。

Any idea? 任何想法?

I see two ways out of your problem: 我认为有两种方法可以解决您的问题:
1) Move SelectBoardCommand inside BoardModel . 1)移动SelectBoardCommand内部BoardModel It won't need a parameter and will operate on each model individually; 它不需要参数,并且可以分别在每个模型上运行;
2) Bind Enabled property of Button to NotUsed property. 2)将Button Enabled属性绑定到NotUsed属性。 In this case, user won't be able to call SelectBoardCommand from UI at all. 在这种情况下,用户将根本无法从UI调用SelectBoardCommand

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM