更改 TextColor &在 Xamarin 的按钮中对 IsEnabled 使用命令 [英] Change TextColor & Use Command on IsEnabled in Button in Xamarin

查看:28
本文介绍了更改 TextColor &在 Xamarin 的按钮中对 IsEnabled 使用命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Xamarin 应用程序的 按钮 上使用 IsEnabled,但有两件事我不能做.

I'm using IsEnabled on Button in Xamarin App and there is 2 things I can't do.

  1. IsEnabled = false 时更改 TextColor,但我可以更改 BackgroundColor.
  1. Change the TextColor when the IsEnabled = false, but I can change the BackgroundColor.

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

The solution is to use the Custom Entry and there is great article for that => https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/entry

但它只适用于:

public class MyEntry : Entry
{
}

我的页面背后的代码是:

and the code behind my Page is:

public class MyEntry : ContentPage
{
}

而且我不能使用多个类.有什么方法可以在 xml.cs 页面中将 EntryContentPage 一起使用?

and also I can't use multiple classes. Is there any way to use the Entry with ContentPage in xml.cs Page?

  1. 我想仅在 IsEnabled = true 时启用 Command,例如ICommand 中的 ViewModel 应该只在 IsEnabled 值为 true 时有效.
  1. I want to enable the Command only when the IsEnabled = true e.g. the ICommand in ViewModel should only works, when the IsEnabled value is 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");
}

推荐答案

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

As when you set the Button IsEnabled="False",it will use a default style of the button.

如果您想使用自定义样式并在 IsEnable 为 true 时处理 Command.您可以在视图模型中定义自定义属性,然后触发您的 命令基于此属性.

If you want to use your custom style and handle the Command when IsEnable is true.you could define a custom property in your viewmodel,and then trigger your Command based on this property.

例如:

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
        }
    
    }
}

然后在您的 Entry TextChanged 事件中:

then in your Entry TextChanged event :

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");
        }
    }

这篇关于更改 TextColor &amp;在 Xamarin 的按钮中对 IsEnabled 使用命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆