【问题标题】:Change TextColor & Use Command on IsEnabled in Button in Xamarin在 Xamarin 的按钮中更改 TextColor 并在 IsEnabled 上使用命令
【发布时间】:2021-02-25 10:09:45
【问题描述】:

我在 Xamarin 应用程序中的 Button 上使用 IsEnabled,但有两件事我不能做。

  1. TextColor改成IsEnabled = false,但是我可以改BackgroundColor

解决方案是使用自定义条目,有一篇很棒的文章 => https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/entry

但它只适用于:

public class MyEntry : Entry
{
}

我的页面背后的代码是:

public class MyEntry : ContentPage
{
}

而且我不能使用多个类。有没有办法在 xml.cs 页面中使用EntryContentPage

  1. 我只想在IsEnabled = true 时启用Command,例如ViewModel 中的 ICommand 只有在 IsEnabled 值为 true 时才有效。

完整的代码示例 => https://stackoverflow.com/a/64808306/14139029

.xml

<Button
    x:Name="PasswordButton"
    IsEnabled="False"
    TextColor="#4DABFE"
    BackgroundColor = "#FFFFFF"
    Text="Submit"
    Command={Binding PasswordButtonCommand}>
</Button>

.xml.cs

if (Password.Text == ConfirmPassword.Text)
{
    PasswordButton.IsEnabled = true;
    PasswordButton.TextColor = Color.FromHex("004B87");
    PasswordButton.BackgroundColor = Color.FromHex("222222");
}

【问题讨论】:

    标签: c# .net xamarin xamarin.forms


    【解决方案1】:
    1. 当 IsEnabled = false 时更改 TextColor,但我可以更改 BackgroundColor。为什么?

    是的,您可以更改 BackgroundColor 但不能更改 TextColor,因为它会在内部覆盖。您必须创建渲染器来更改文本颜色。

    1. 当我使用 Command 时,IsEnabled 变为 true。 IsEnabled 那时不起作用。 IsEnabled 的代码写在 .xml.cs 页面中。是 有没有可能将 Command 与 IsEnabled 一起使用?

    command 和 IsEnabled 属性之间没有关系,所以它应该可以工作。

    Xaml.cs

    <Button
        x:Name="PasswordButton"
                Clicked="PasswordButton_Clicked"
        IsEnabled="False" Command="{Binding PasswordButtonCommand}"
        Text="Submit">
            </Button>
    

    ViewModel.cs

    public class ViewModel : INotifyPropertyChanged
        {
            
            public ICommand PasswordButtonCommand => new Command(Submit);
    
            private void Submit(object obj)
            {
                Debug.WriteLine("Test");
            }
    
            public ViewModel()
            {
                
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected virtual void OnPropertyChanged(
            [CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this,
                new PropertyChangedEventArgs(propertyName));
            }
        }
    

    【讨论】:

      【解决方案2】:

      当您设置按钮IsEnabled="False"时,它将使用按钮的默认样式。

      如果您想在IsEnable 为真时使用您的自定义样式并处理Command。您可以在您的视图模型中定义一个自定义属性,然后根据该属性触发您的Command

      例如:

      public class ViewModel
      {
      
          public ICommand PasswordButtonCommand => new Command<Button>(Submit);
          public bool IsEnable { get; set; }
      
          private void Submit(object obj)
          {
      
              if (IsEnable)
              {
                   //do something you want
              }
          
          }
      }
      

      然后在您的条目TextChanged 事件中:

      private void Entry_TextChanged(object sender, TextChangedEventArgs e)
          {
              if (Password.Text == ConfirmPassword.Text)
              {
                  viewModel.IsEnable = true;
                  PasswordButton.TextColor = Color.FromHex("004B87");
                  PasswordButton.BackgroundColor = Color.FromHex("222222");
              }
              else
              {
                  viewModel.IsEnable = false;
                  PasswordButton.TextColor = Color.FromHex("4DABFE");
                  PasswordButton.BackgroundColor = Color.FromHex("FFFFFF");
              }
          }
      

      【讨论】:

      • 我在viewModel.IsEnable = true; 上遇到了错误,An object reference is required for the non-static field, method, or property 'viewModel.IsEnable'.. 所以,我将public bool IsEnable { get; set; } 设为静态,它工作了...非常感谢
      猜你喜欢
      • 1970-01-01
      • 2013-03-31
      • 1970-01-01
      • 1970-01-01
      • 2011-12-09
      • 1970-01-01
      • 2014-12-20
      • 2014-06-26
      • 2019-12-21
      相关资源
      最近更新 更多