【发布时间】:2023-02-05 18:21:07
【问题描述】:
我的 Xamarin.Forms 应用程序中有此表单,其中有两个按钮,它们都用于更新布尔值。根据该值是真还是假,我只想启用其中一个按钮。将它们想象成一扇“门”:一个按钮将“退出”布尔值设置为真,另一个设置为“假”。因此,当单击“输入”按钮时,我希望它被禁用,直到用户通过单击“退出”按钮“退出”。
CanExecute/ChangeCanExecute 应该是去这里的方式,至少根据我自己的知识 - 这就是我尝试过的。 但它似乎不起作用,即使我在更简单的内容页面上抽象了该功能。
我附上了我的 ViewModel 代码示例,为清楚起见进行了简化。
我不明白为什么我会被 MVVM 约定之外如此简单的东西难倒。
public bool _hasWorkerExit;
public bool hasWorkerExit
{
get { return _hasWorkerExit; }
set
{
_hasWorkerExit = value;
OnPropertyChanged();
EnterCommand?.ChangeCanExecute();
ExitCommand?.ChangeCanExecute();
}
}
public Command EnterCommand => new Command(SendWorkerEntry,WorkerCanEnter());
public Command ExitCommand => new Command(SendWorkerExit,WorkerCanExit());
private Func<bool> WorkerCanEnter()
{
return new Func<bool>(() => hasWorkerExit);
}
private Func<bool> WorkerCanExit()
{
return new Func<bool>(() => !hasWorkerExit);
}
private void SendWorkerEntry()
{
// Do the work you're meant to do
hasWorkerExit = false;
}
private void SendWorkerExit()
{
// Do the work you're meant to do
hasWorkerExit = true;
}
这是按钮的 .xaml 代码
<dxe:SimpleButton Grid.Column="0"
FontSize="13"
Text="Enter"
BorderThickness="0"
BackgroundColor="{StaticResource ButtonColour}"
PressedBackgroundColor="{StaticResource PressedButtonColour}"
TextColor="{StaticResource ButtonTextColour}"
PressedTextColor="{StaticResource ButtonTextColour}"
DisabledBackgroundColor="{StaticResource DisabledButtonColour}"
CornerRadius="0"
CornerMode="Round"
Command="{Binding EnterCommand}"></dxe:SimpleButton>
<dxe:SimpleButton Grid.Column="1"
FontSize="13"
Text="Exit"
BorderThickness="0"
BackgroundColor="{StaticResource ButtonColour}"
PressedBackgroundColor="{StaticResource PressedButtonColour}"
TextColor="{StaticResource ButtonTextColour}"
PressedTextColor="{StaticResource ButtonTextColour}"
DisabledBackgroundColor="{StaticResource DisabledButtonColour}"
CornerRadius="0"
CornerMode="Round"
Command="{Binding ExitCommand}"></dxe:SimpleButton>
【问题讨论】:
-
您可以显示按钮的视图部分 [.xaml] 吗?
-
我已经编辑了我的问题以包含 .xaml - 太大而无法在此处发布 - 因为事实证明拼写错误不是唯一的问题......
标签: c# xamarin xamarin.forms