如果用户更改了AppTheme,我想让标题栏按钮的ForegroundColor
动态显示。
我使用“Windows Template Studio”创建了应用,以获取更改AppTheme
的设置。
我宣布遵循XAML:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="CustomColor">#FF6D0839</Color>
<SolidColorBrush x:Key="ThemeBaseLowColorBrush" Color="{ThemeResource SystemBaseLowColor}" />
<SolidColorBrush x:Key="ThemeControlForegroundBaseHighBrush" Color="{ThemeResource SystemBaseHighColor}" />
<SolidColorBrush x:Key="ThemeControlBackgroundBaseHighBrush" Color="{ThemeResource SystemAccentColor}" />
<SolidColorBrush x:Key="SystemControlHighlightAccentBrush" Color="{ThemeResource SystemBaseMediumLowColor}" />
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Default">
<Color x:Key="SystemBaseMediumLowColor">Red</Color>
</ResourceDictionary>
<ResourceDictionary x:Key="Light">
<Color x:Key="SystemBaseMediumLowColor">Green</Color>
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<Color x:Key="SystemBaseMediumLowColor">Blue</Color>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
在后面的代码中:
public static void SetRequestedTheme()
{
if (Window.Current.Content is FrameworkElement frameworkElement)
{
frameworkElement.RequestedTheme = Theme;
ApplicationViewTitleBar titleBar = ApplicationView.GetForCurrentView().TitleBar;
titleBar.ButtonForegroundColor = (Color)Application.Current.Resources["SystemBaseMediumLowColor"];
}
}
不幸的是titlebar.ButtonForegroundColor
仍然是一样的。我将始终获得光源的颜色,而不是所选资源的颜色。
有什么想法吗?
P.S。:我也尝试过使用UWP.Toolkit
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:Microsoft.Toolkit.Uwp.SampleApp.SamplePages"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:extensions="using:Microsoft.Toolkit.Uwp.UI.Extensions"
extensions:ApplicationView.Title="View Extensions"
extensions:TitleBar.ButtonForegroundColor="White"
extensions:StatusBar.BackgroundColor="{ThemeResource SystemBaseMediumLowColor}"
mc:Ignorable="d">
这样做会抛出XAML例外。当我使用StaticResource
而不是它有用吗?
非常感谢你的帮助
P.S。:我最终是这样的:
public static void SetRequestedTheme()
{
if (Window.Current.Content is FrameworkElement frameworkElement)
{
frameworkElement.RequestedTheme = Theme;
Color color;
var appTheme = Application.Current.RequestedTheme;
switch (Theme)
{
case ElementTheme.Default:
color = ((Color)Application.Current.Resources["SystemBaseHighColor"]);
break;
case ElementTheme.Light:
if (appTheme == ApplicationTheme.Light) { color = ((Color)Application.Current.Resources["SystemBaseHighColor"]); }
else { color = ((Color)Application.Current.Resources["SystemAltHighColor"]); }
break;
case ElementTheme.Dark:
if (appTheme == ApplicationTheme.Light) { color = ((Color)Application.Current.Resources["SystemAltHighColor"]); }
else { color = ((Color)Application.Current.Resources["SystemBaseHighColor"]); }
break;
default:
break;
}
ApplicationViewTitleBar titleBar = ApplicationView.GetForCurrentView().TitleBar;
titleBar.ButtonForegroundColor = color;
}
}
在ApplicationTheme中存储用户的系统设置,该设置由“默认”ElementTheme激活
取决于ApplicatinTheme((Color)Application.Current.Resources [“SystemBaseHighColor”])的结果是不同的,所以我需要检查一个选择正确的资源
答案 0 :(得分:0)
您可以使用UWP中的UISettings
类来观察颜色和主题更改。我之前写过blog post about这个。简单地说,您可以观察UISettings.ColorValuesChanged
事件以查看颜色或主题何时发生变化并对其做出反应。另请参阅此nice helper class on GitHub,它还处理窗口激活(因为当应用程序在后台时不会触发事件)。
至于UWP社区工具包扩展的ThemeResource
设置不起作用的原因 - 由于TitleBar
上的颜色是静态设置的,因此必须通过代码完成,而{{1} }是仅限XAML的标记扩展,这意味着它与此不兼容。