我需要在禁用按钮时更改按钮的文本颜色,我为 iOS 创建了一个自定义渲染器,为 Android 创建了一个。 iOS 完美运行,因为 android 不会改变颜色,我也通过样式创建了触发器,但也没有解决。
如何使颜色交换适用于 Xamarin.Forms?
安卓渲染器:
[assembly: ExportRenderer(typeof(Button), typeof(MyButtonRenderer))]
namespace xxxxxx.Droid.Renderers
{
public class MyButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if (Control != null)
Control.SetTextColor(Color.White.ToAndroid());
if (e != null)
{
e.NewElement.TextColor = Color.White;
e.OldElement.TextColor = Color.White;
}
}
}
}
此状态更改是根据我的命令的 CanExecute 更改的,这将是应用的默认样式。
这两种方式都没有解决
最佳答案
我使用 VisualStateManager为每个按钮状态添加不同的样式:正常、聚焦、按下、禁用
<Style TargetType="Button">
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="{StaticResource ColorAccent}" />
<Setter Property="TextColor" Value="{StaticResource ColorForegroundIcon}" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="{StaticResource ColorAccentTransparent}" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Focused">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="{StaticResource ColorAccentTransparent}" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Disabled">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="{DynamicResource ColorBackgroundDisabled}" />
<Setter Property="TextColor" Value="{DynamicResource ColorForegroundFaded}" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
有关更多信息和示例,请查看:Understanding Visual State Manager in Xamarin Forms
关于c# - 更改禁用按钮中的按钮文本颜色 (Xamarin.Forms),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46981060/