简体   繁体   English

如何更改颜色以调整 UWP 中的主题设置?

[英]How to change color to adjust theme setting in UWP?

Simply want to set Foreground color of a TextBlock to LightGreen in dark mode, set to DarkGreen in light mode.只是想在深色模式下将TextBlockForeground设置为LightGreen ,在浅色模式下设置为DarkGreen There is a theme setting in my app.我的应用程序中有一个主题设置。 I want the color adjust both system setting and app setting.我想要颜色调整系统设置和应用程序设置。

I tried this when page loaded:我在页面加载时尝试了这个:

var isDark = ThemeSelectorService.Theme == ElementTheme.Dark;
if(ThemeSelectorService.Theme == ElementTheme.Default)
{
   isDark = Application.Current.RequestedTheme == ApplicationTheme.Dark;
}
PriceText.Foreground = new SolidColorBrush(isDark ? Colors.LightGreen : Colors.DarkGreen);
<TextBlock x:Name="PriceText" Text="This is a Text"/>

but when I change the system setting, the color will not change until reload to this page, how to make it happen?但是当我更改系统设置时,颜色不会改变,直到重新加载到此页面,如何实现?

When system color changes, the page loaded method will not be called.当系统颜色发生变化时,不会调用页面加载方法。 You could use UISettings.ColorValuesChanged Event to monitor the changes of system color.您可以使用UISettings.ColorValuesChanged 事件来监控系统颜色的变化。

when I change the system setting, the color will not change until reload to this page当我更改系统设置时,颜色不会改变,直到重新加载到此页面

If you change the color of your app by changing the value of FrameworkElement.RequestedTheme property which can override the app-level RequestedTheme , you need to change the value of the FrameworkElement.RequestedTheme property to ElementTheme.Default to let the FrameworkElement.RequestedTheme use the Application.RequestedTheme without reloading the page.如果您通过更改可以覆盖应用程序级RequestedThemeFrameworkElement.RequestedTheme 属性的值来更改应用程序的颜色,则需要将FrameworkElement.RequestedTheme属性的值更改为ElementTheme.Default以让FrameworkElement.RequestedTheme使用Application.RequestedTheme无需重新加载页面。

Please check the following code to let the color adjust system setting:请检查以下代码以让颜色调整系统设置:

public MainPage()
{
    this.InitializeComponent();
    ……
    uiSettings.ColorValuesChanged += UiSettings_ColorValuesChanged;
}

private async void UiSettings_ColorValuesChanged(UISettings sender, object args)
{
    var isDark=false;
    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
        //Change the RequestedTheme of elements to ElementTheme.Default
        // if you have changed some elements' RequestedTheme property
        RequestedTheme = ElementTheme.Default;
        ThemeSelectorService.Theme = ElementTheme.Default;

        isDark = Application.Current.RequestedTheme == ApplicationTheme.Dark;
        PriceText.Foreground = new SolidColorBrush(isDark ? Colors.Green : Colors.Red);
    });
}

Answer from YanGu严谷的回答

Step 1: add ResourceDictionary.ThemeDictionaries to ResourceDictionary第 1 步:将 ResourceDictionary.ThemeDictionaries 添加到 ResourceDictionary

<ResourceDictionary.ThemeDictionaries>
    <ResourceDictionary x:Key="Light">
        <SolidColorBrush x:Key="PriceBrush" Color="DarkGreen"/>
    </ResourceDictionary>
    <ResourceDictionary x:Key="Dark">
        <SolidColorBrush x:Key="PriceBrush" Color="LightGreen"/>
    </ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>

Step 2: use the SolidColorBrush for your usercontrol第 2 步:为您的用户控件使用 SolidColorBrush

<TextBlock Text="This is a text" Foreground="{ThemeResource PriceBrush}"/>

This is pretty easy but cannot be found in other questions.这很容易,但在其他问题中找不到。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM