如果用户更改应用程序主题,我希望使标题栏按钮的 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
代替时它可以工作吗?
非常感谢您的帮助
PS:我最终是这样的:
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"]) 的结果有所不同,因此我需要检查并选择正确的资源
最佳答案
您可以使用 UWP 中的 UISettings
类观察颜色和主题变化。我之前写过blog post about这。简而言之,您可以观察 UISettings.ColorValuesChanged
事件来查看颜色或主题何时发生变化并使用react。另请参阅此 nice helper class on GitHub ,它还处理窗口激活(因为当应用程序处于后台时不会触发该事件)。
至于 UWP Community Toolkit 扩展的 ThemeResource
设置不起作用的原因 - 由于颜色是在 TitleBar
上静态设置的,因此必须通过代码完成,而 ThemeResource
是仅 XAML 标记扩展,这意味着它与此不兼容。
关于uwp:使用 ThemeResource 更改 titleBar.ButtonForegroundColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48201278/