c# - 更改禁用按钮中的按钮文本颜色 (Xamarin.Forms)

标签 c# xamarin xamarin.forms xamarin.android

我需要在禁用按钮时更改按钮的文本颜色,我为 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/

相关文章:

ios - 如何在 iOS 上旋转后修复 Xamarin Forms 中的 WebView 布局(WKWebView)

xaml - WebView GestureRecognition 在 Xamarin 表单中不起作用

c# - 在运行时合并 WPF ResourceDictionary

c# - 如何使用 Entity Framework 6 更新记录?

c# - 四个可为 null 的 bool 多选复选框。

android - 从 Application.Context 获取当前 Activity - MonoAndroid

c# - 半透明图片框c#

android - 在 Fragment 中添加 SearchView

C# - 使用 Refit 传递 Web Api 对象

c# - Master和Detail must be set before adding MasterDetailPage to a container 错误在Xamarin forms中