简体   繁体   English

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

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

I am developing a Xamarin.Forms UWP application.我正在开发一个Xamarin.Forms UWP 应用程序。

I am struggling to set the accent colour of my application.我正在努力设置我的应用程序的强调色。 This is the colour that is used for certain behaviors by default on controls.这是默认情况下在控件上用于某些行为的颜色。

For example the Entry control has a default blue highlighting on focus shown below:例如,Entry 控件在焦点上有一个默认的蓝色突出显示,如下所示:

在此处输入图像描述

I have tried a few suggestions from this thread: Change Accent Color in Windows 10 UWP but none seemed to work.我已经尝试了这个线程的一些建议: 在 Windows 10 UWP 中更改强调色,但似乎都没有用。

I am not sure whether it is because I didn't fully understand how changing the colour for UWP differs for Xamarin.UWP, or whether what I'm trying to do is even possible with Xamarin.Forms.我不确定是否是因为我没有完全理解 UWP 的颜色更改与 Xamarin.UWP 有何不同,或者我正在尝试做的事情是否甚至可以用于 Xamarin.Forms。

Has anyone found out how to do this?有没有人知道如何做到这一点?

Here is the style code of FormsTextBox for UWP. 是UWP的FormsTextBox的样式代码。

You need to override below styled colors: 您需要覆盖以下样式的颜色:

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

So to change the colour of your textbox boarder brush you can add these ThemeResources to your App.xaml like so: 因此,要更改文本框边框画笔的颜色,您可以将这些ThemeResources添加到App.xaml如下所示:

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

You can define a style setter property in App.xaml 您可以在App.xaml中定义样式设置器属性

    <ResourceDictionary>    
        <Style TargetType="Button" x:Name="myNewButtonStyle">
            <Setter Property="Background" Value="{ThemeResource ButtonBackgroundThemeBrush}" />
        </Style>
    </ResourceDictionary>

Then use CustomRenderer for the controls you need to change colors 然后使用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;
        }
    }

in a similar way you would be able to use Themed Resource Dictionary key and apply. 以类似的方式,您将能够使用主题资源字典键并应用。 This code can be used to have native styles on Xamarin's control. 此代码可用于在Xamarin的控件上具有本机样式。

The absolute easiest way is to go to the styles.xml file which is located in Resources/values and uncomment:绝对最简单的方法是将 go 转到位于 Resources/values 中的styles.xml文件并取消注释:

<item name="colorAccent">#FF2081</item>

This changes the accent color on the whole project eg with Entrys.这会改变整个项目的强调色,例如条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使图像显示在我的 Xamarin.Forms 应用程序的 UWP 版本中 - How do I make images show up in UWP version of my Xamarin.Forms application 如何更改Xamarin.Forms中的应用程序图标? - How to change application icon in Xamarin.Forms? 如何隐藏Xamarin.Forms UWP中的默认工具栏按钮(三个点) - How do I hide the default toolbar button (three dots) in Xamarin.Forms UWP 如何从 Xamarin.Forms 应用程序执行 SAML 2.0 身份验证 - How to do SAML 2.0 Auth from Xamarin.Forms application Xamarin.Forms 更改应用程序的背景颜色 - Xamarin.Forms Change Background Color For Application 如何更改Xamarin.Forms中编辑器控件的OnMouseHover行为 - How do I change the OnMouseHover behavior of an Editor Control in Xamarin.Forms 如何在运行时在 Xamarin.Forms 中动态更改图像的位置(x 和 y)? - How do I change an image's position (x and y) dynamically at runtime in Xamarin.Forms? 更改汉堡菜单图标 - Xamarin.Forms Android & UWP - Change hamburger menu icon - Xamarin.Forms Android & UWP 如何在Xamarin.Forms中更改页面? - How can I change the Page in Xamarin.Forms? 如何使用自定义渲染器更改 Xamarin.Forms、Android、iOs、UWP 上的滑块颜色? - How to Change the Color of Slider on Xamarin.Forms, on Android, iOs, UWP using custom renderers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM