【问题标题】:How do I change the accent colour of a Xamarin.Forms UWP application?如何更改 Xamarin.Forms UWP 应用程序的强调色?
【发布时间】:2016-10-29 06:02:28
【问题描述】:

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

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

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

我已经尝试了来自该线程的一些建议:Change Accent Color in Windows 10 UWP,但似乎没有一个有效。

我不确定是否是因为我不完全了解更改 UWP 的颜色对于 Xamarin.UWP 有何不同,或者我尝试做的事情是否甚至可以使用 Xamarin.Forms。

有人知道怎么做吗?

【问题讨论】:

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


    【解决方案1】:

    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>
    

    【讨论】:

    • 您是说我需要自定义构建 Xamarin.Forms 还是需要覆盖此处显示的 ThemeResource 值?如果是这样,我将如何覆盖这些值?
    • 另外,XAML 是唯一的方法吗?有没有办法在代码中实现这一点?
    【解决方案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 的控件上具有本机样式。

    【讨论】:

    • 这个想法还不错,但对于TextBox 来说,您似乎没有可更改的属性,例如边框颜色。
    猜你喜欢
    • 2021-07-21
    • 1970-01-01
    • 2021-04-02
    • 2019-11-23
    • 2018-06-28
    • 2015-10-28
    • 2020-12-18
    • 2016-10-23
    • 1970-01-01
    相关资源
    最近更新 更多