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

时间:2016-06-27 09:27:35

标签: c# xamarin xamarin.forms uwp windows-10-universal

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

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

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

enter image description here

我从这个帖子中尝试了一些建议:Change Accent Color in Windows 10 UWP但似乎都没有。

我不确定是否因为我没有完全理解为Xamarin.UWP改变UWP颜色的方式有何不同,或者我是否可以使用Xamarin.Forms进行尝试。

有没有人发现如何做到这一点?

2 个答案:

答案 0 :(得分:4)

Here是UWP的FormsTextBox样式代码。

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

<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>

答案 1 :(得分:2)

您可以在App.xaml中定义样式设置器属性

    <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的控件上具有原生样式。