更改 TextColor &在 Xamarin 的按钮中对 IsEnabled 使用命令 [英] Change TextColor & Use Command on IsEnabled in Button in Xamarin
问题描述
我在 Xamarin 应用程序的 按钮 上使用 IsEnabled
,但有两件事我不能做.
I'm using IsEnabled
on Button in Xamarin App and there is 2 things I can't do.
- 在
IsEnabled = false
时更改TextColor
,但我可以更改BackgroundColor
.
- Change the
TextColor
when theIsEnabled = false
, but I can change theBackgroundColor
.
解决方案是使用自定义条目,并且有一篇很棒的文章=>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 页面中将 Entry
与 ContentPage
一起使用?
and also I can't use multiple classes. Is there any way to use the Entry
with ContentPage
in xml.cs Page?
- 我想仅在
IsEnabled = true
时启用Command
,例如ICommand
中的 ViewModel 应该只在IsEnabled
值为true
时有效.
- I want to enable the
Command
only when theIsEnabled = true
e.g. theICommand
in ViewModel should only works, when theIsEnabled
value istrue
.
完整的代码示例 =>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 &在 Xamarin 的按钮中对 IsEnabled 使用命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!