我想在禁用Text.Color
时更改Button
。我该怎么办?
Xaml按钮:
<Button Text="-" TextColor="White" Padding="0" CornerRadius="4" FontAttributes="Bold" FontSize="Small" IsEnabled="False" BackgroundColor="#3F3C42" Clicked="QntLabelLess" x:Name="QntLess" Grid.Column="1" Grid.Row="1"/>
隐藏代码:
private void QntLabelLess(object sender, EventArgs e)
{
if (Qnt == 2)
{
QntLess.IsEnabled = false;
// QntLess.TextColor = Color.FromHex("#FFFFFF");
QntLess.BackgroundColor = Color.FromHex("#3F3C42");
}
if (Qnt == 10)
{
QntPlus.IsEnabled = true;
QntPlus.TextColor = Color.FromHex("#000000");
QntPlus.BackgroundColor = Color.FromHex("#FF8A00");
}
Qnt--;
QntLabel.Text = Qnt.ToString();
}
我知道这个问题有很多无用的代码,但是我更喜欢将所有方法代码都放进去。
答案 0 :(得分:0)
如果我正确阅读此内容,则该按钮将以禁用状态启动。因此,您的click事件将永远不会被调用,因为该按钮已被禁用。我猜您只能在禁用状态下看到该按钮,而从不在启用状态下。
答案 1 :(得分:0)
如果要实现此目标,则应实现按钮的自定义渲染器。 这里正在运行GIF。
对于Android。
[assembly: ExportRenderer(typeof(Xamarin.Forms.Button), typeof(MyButtonRenderer))]
namespace ButtonStyle.Droid
{
public class MyButtonRenderer : ButtonRenderer
{
public MyButtonRenderer(Context context) : base(context) { }
protected override void
OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> args)
{
base.OnElementChanged(args);
if (Control != null) SetColors();
}
protected override void OnElementPropertyChanged(object sender,
PropertyChangedEventArgs args)
{
base.OnElementPropertyChanged(sender, args);
if (args.PropertyName == nameof(Xamarin.Forms.Button.IsEnabled)) SetColors();
}
private void SetColors()
{
Control.SetTextColor(Element.IsEnabled ? Element.TextColor.ToAndroid() :
Android.Graphics.Color.Red);
Control.SetBackgroundColor(Element.IsEnabled ?
Element.BackgroundColor.ToAndroid() : Android.Graphics.Color.Purple);
}
}
}
对于IOS(我今天没有带Mac,您最好自己进行测试)
[assembly: ExportRenderer(typeof(Button), typeof(MyButtonRenderer))]
namespace ButtonStyle.iOS
{
public class MyButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> args)
{
base.OnElementChanged(args);
if (Control != null) SetColors();
}
protected override void OnElementPropertyChanged(object sender,
PropertyChangedEventArgs args)
{
base.OnElementPropertyChanged(sender, args);
if (args.PropertyName == nameof(Button.IsEnabled)) SetColors();
}
private void SetColors()
{
Control.SetTitleColor(Element.IsEnabled ? Element.TextColor.ToUIColor() :
UIColor.Red, Element.IsEnabled ? UIControlState.Normal :
UIControlState.Disabled);
Control.BackgroundColor = Element.IsEnabled ?
Element.BackgroundColor.ToUIColor() : UIColor.Purple;
}
}
有代码MainPage.xaml
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:ButtonStyle"
x:Class="ButtonStyle.MainPage">
<StackLayout>
<Label x:Name="QntLabel" Text="1"/>
<!-- Place new controls here -->
<Button Text="------------------------" TextColor="White" Padding="0" CornerRadius="4" FontAttributes="Bold" FontSize="Small" IsEnabled="True" BackgroundColor="#3F3C42" Clicked="QntLabelLess" x:Name="QntLess"/>
<Button Text="++++++++++++++++++++++++" TextColor="White" Padding="0" CornerRadius="4" FontAttributes="Bold" FontSize="Small" IsEnabled="True" BackgroundColor="#3F3C42" Clicked="QntLabelPlus" x:Name="QntPlus"/>
</StackLayout>
有MainPage.xaml.cs的代码
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void QntLabelLess(object sender, EventArgs e)
{
int Qnt=2;
if (Qnt == 2)
{
// QntLess.TextColor = Color.FromHex("#FF0000");
// QntLess.BackgroundColor = Color.FromHex("#3F3C42");
QntLess.IsEnabled = false;
}
if (Qnt == 10)
{
QntPlus.IsEnabled = true;
QntPlus.TextColor = Color.FromHex("#FF0000");
QntPlus.BackgroundColor = Color.FromHex("#FF8A00");
}
Qnt--;
QntLabel.Text = Qnt.ToString();
}
private void QntLabelPlus(object sender, EventArgs e)
{
}
}