【问题标题】:How to change color to adjust theme setting in UWP?如何更改颜色以调整 UWP 中的主题设置?
【发布时间】:2021-03-21 00:40:49
【问题描述】:

只想在深色模式下将TextBlockForeground 颜色设置为LightGreen,在浅色模式下设置为DarkGreen。我的应用程序中有一个主题设置。我想要颜色调整系统设置和应用设置。

我在页面加载时尝试过:

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

但是当我更改系统设置时,颜色不会改变,直到重新加载到此页面,如何实现?

【问题讨论】:

  • 您能告诉我 ThemeSelectorService.Theme 代表什么,它代表 PriceText.RequestTheme 属性吗?如果您能提供有关如何在代码隐藏中更改主题的代码,那就更好了。
  • ThemeSelectorService.Theme 代表RequestedTheme 代表Window.Current.Content

标签: c# windows xaml uwp


【解决方案1】:

当系统颜色发生变化时,不会调用页面加载方法。您可以使用UISettings.ColorValuesChanged Event 来监控系统颜色的变化。

当我更改系统设置时,颜色不会改变,直到重新加载到此页面

如果您通过更改FrameworkElement.RequestedTheme property 的值来更改应用程序的颜色,这可以覆盖应用程序级别的RequestedTheme,您需要将FrameworkElement.RequestedTheme 属性的值更改为ElementTheme.Default 以让@ 987654328@ 使用Application.RequestedTheme 无需重新加载页面。

请检查以下代码让颜色调整系统设置:

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);
    });
}

【讨论】:

  • 它对我有用。但仍然想知道是否有更简单的方法来创建主题画笔,就像 ApplicationForegroundThemeBrush
  • 你可以使用ThemeResource在主题改变时改变TextBlock控件的前景。如果您在 UISettings.ColorValuesChanged 事件处理程序中更改了某些元素的 RequestedTheme 属性,请注意将元素的 RequestedTheme 更改为 ElementTheme.Default。
  • 例如&lt;ResourceDictionary.ThemeDictionaries&gt;&lt;ResourceDictionary x:Key="Light"&gt; &lt;SolidColorBrush x:Key="foregroundBrush" Color="Red"/&gt; &lt;/ResourceDictionary&gt; &lt;ResourceDictionary x:Key="Dark"&gt; &lt;SolidColorBrush x:Key="foregroundBrush" Color="Green"/&gt; &lt;/ResourceDictionary&gt;&lt;/ResourceDictionary.ThemeDictionaries&gt; …… &lt;TextBlock Foreground="{ThemeResource foregroundBrush}" &gt;Hello world&lt;/TextBlock&gt;
  • 是的,这正是我想要的!
【解决方案2】:

燕谷的回答

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

第 2 步:为您的用户控件使用 SolidColorBrush

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

这很简单,但在其他问题中找不到。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 2018-08-18
    • 2020-01-22
    • 2022-08-18
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多