Xamarin MVVM 按钮绑定

问题描述

我已经被这个问题困扰了两天了。我想在使用数据绑定时一键禁用按钮。首先,我的 viewmodel 检查是否有可用的免费停车位,然后如果有 0 个可用的停车位,则在调用构造函数后立即禁用我的 Button。如果有可用的停车位,按一下后我想减少可用的停车位,这很好用,但我的 Button 仍然保持启用状态,所以我应该如何在按一下后禁用它。 我的 viewmodel 类:

public class SpacesPageviewmodel : INotifyPropertyChanged
{
    public int CurrentSpaces;
    public ICommand MyCommand { private set; get; }
    // BANDAU NAUJA DALYKA
    public ICommand myCommand
    {
        set
        {
            if(EmptySpaces == 0)
            {
                MyCommand.CanExecute(false);
                PropertyChanged?.Invoke(this,new PropertyChangedEventArgs("myCommand"));
            }
        }
        get
        {
            return MyCommand;
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    public ObservableCollection<Location> AllSpaces { get; set; }
    public ObservableCollection<Location> allSpaces { get { return AllSpaces; } }
    public int EmptySpaces { get; set; }
    public int emptySpaces
    {
        set
        {
            if(EmptySpaces != value)
            {
                EmptySpaces = value;
                PropertyChanged?.Invoke(this,new PropertyChangedEventArgs("emptySpaces"));
            }
        }
        get
        {
            return EmptySpaces;
        }
    }

    public SpacesPageviewmodel(string street)
    {
        var result = LocationsDb.Locations
            .Where(x => x.street == street)
            .Select(g => g.emptySpaces)
            .FirstOrDefault();

        MyCommand = new Command(() => 
        {
            foreach (var item in LocationsDb.Locations.Where(x => x.emptySpaces == EmptySpaces))
            {
                if (item.emptySpaces > 0)
                {
                    item.emptySpaces--;
                    emptySpaces = item.emptySpaces;
                    myCommand = MyCommand;
                    break;
                }
            }
        },() => 
        {
            if (EmptySpaces == 0)
                return false;
            else
                return true;
        });
        EmptySpaces = result;
        CurrentSpaces = result;
    }
}

我的 XAML:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="ParkingApp.Views.SpacesPage"
         ControlTemplate="{StaticResource MyTemplate}"
         >
<ContentPage.Content>
    <Grid>
        <Grid.RowDeFinitions>
            <RowDeFinition Height="auto"/>
            <RowDeFinition Height="1*"/>
            <RowDeFinition Height="auto"/>
            <RowDeFinition Height="auto"/>
        </Grid.RowDeFinitions>

        <Label Grid.Row="0" Text="Reserve parking spot" 
               BackgroundColor="#e8e8e8" HorizontalTextAlignment="Center" 
               HeightRequest="50" VerticalTextAlignment="Center"
               FontSize="30"/>

        <Label Grid.Row="1" Text="{Binding emptySpaces}" FontSize="150" TextColor="Black" Margin="10"
               HorizontalOptions="CenterandExpand" VerticalOptions="CenterandExpand"/>
        <Label Grid.Row="2" Text="Free Spaces Left" TextColor="Black" FontSize="35" VerticalOptions="CenterandExpand"
               HorizontalOptions="CenterandExpand"/>
        <Button Grid.Row="3" Margin="10" Text="RESERVE" Command="{Binding MyCommand}" FontSize="20"/>
    </Grid>
</ContentPage.Content>

解决方法

我认为问题出在 myCommand 属性的设置器中。 MyCommand.CanExecute(false); 行不会禁用按钮,而是使用参数 false 调用 CanExecuteMethod。我猜您的意图是调用 ChangeCanExecute() 方法,以通知命令状态可以更改。因此,将这一行替换为:

((Command) MyCommand)).ChangeCanExecute();