【问题标题】:Xamarin Forms - toggle between button statesXamarin Forms - 在按钮状态之间切换
【发布时间】: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


【解决方案1】:

你可以试试:

按钮 1:

IsEnabled="{Binding HasWorkerExit}"

按钮 2:

IsEnabled="{Binding HasWorkerExit, Converter={Helpers:InverseBoolConverter}}"}"

仅一项财产就可以解决您的问题。

public bool _hasWorkerExit;
    public bool HasWorkerExit
    {
        get { return _hasWorkerExit; }
        set
        {
            _hasWorkerExit = !_hasWorkerExit;
            OnPropertyChanged();
        }
    }

反转助手代码:

public class InverseBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return !((bool)value);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
        //throw new NotImplementedException();
    }
}

【讨论】:

  • 我已经使用了一个属性。另外,当您使用 MVVM 命令时,IsEnabled 属性意味着 zilch,所以恐怕这不是这里的解决方案。
猜你喜欢
  • 2020-09-20
  • 1970-01-01
  • 2011-06-16
  • 1970-01-01
  • 1970-01-01
  • 2017-05-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多