如何更改 Xamarin.Forms UWP 应用程序的强调色?

How do I change the accent colour of a Xamarin.Forms UWP application?

我正在开发 Xamarin.Forms UWP 应用程序。

我正在努力设置我的应用程序的强调色。这是控件上默认用于某些行为的颜色。

例如,Entry 控件在焦点上有一个默认的蓝色高亮显示,如下所示:

我已经尝试了此线程中的一些建议: 但 none 似乎有效。

我不确定是不是因为我没有完全理解 Xamarin.UWP 改变 UWP 的颜色有何不同,或者我想做的事情是否甚至可以用 Xamarin.Forms.

有人知道怎么做吗?

Here is the style code of FormsTextBox 用于 UWP。

您需要覆盖以下样式颜色:

<Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}" />
<Setter Property="Background" Value="{ThemeResource SystemControlBackgroundAltHighBrush}" />
<Setter Property="BackgroundFocusBrush" Value="{ThemeResource SystemControlBackgroundChromeWhiteBrush}" />
<Setter Property="BorderBrush" Value="{ThemeResource SystemControlForegroundChromeDisabledLowBrush}" />

因此,要更改文本框边框画笔的颜色,您可以将这些 ThemeResources 添加到 App.xaml,如下所示:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.ThemeDictionaries>
            <ResourceDictionary x:Key="Light">
                <SolidColorBrush x:Key="SystemControlHighlightAccentBrush" Color="#ff0000" />
            </ResourceDictionary>
        </ResourceDictionary.ThemeDictionaries>
    </ResourceDictionary>
</Application.Resources>

您可以在 App.xaml

中定义样式 setter 属性
    <ResourceDictionary>    
        <Style TargetType="Button" x:Name="myNewButtonStyle">
            <Setter Property="Background" Value="{ThemeResource ButtonBackgroundThemeBrush}" />
        </Style>
    </ResourceDictionary>

然后对需要更改颜色的控件使用 CustomRenderer

    protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
    {
        base.OnElementChanged(e);
        if (this.Element != null)
        {
            this.Control.Style = Windows.UI.Xaml.Application.Current.Resources["myNewButtonStyle"] as Windows.UI.Xaml.Style;
        }
    }

以类似的方式,您可以使用主题资源词典键并申请。此代码可用于在 Xamarin 的控件上具有原生样式。