[英]How to change uwp app title bar color when Windows 10 change from dark theme to light and vice versa?
I have an app for test, with 3 buttons:我有一个测试应用程序,有 3 个按钮:
Those first two buttons are ok and working, but the last one I don't know how to implement and where to put methods if necessary.前两个按钮可以正常工作,但最后一个我不知道如何实现以及在必要时将方法放在哪里。 I'd like, please, any help, tip or even a whole solution for the problem.
我想要任何帮助、提示,甚至是针对该问题的完整解决方案。 Thank you in advance.
先感谢您。 PS: would be great if title bar color can follow dark, light and even high contrast colors.
PS:如果标题栏颜色可以跟随深色,浅色甚至高对比度colors,那就太好了。
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace App7
{
public sealed partial class MainPage : Page
{
Windows.UI.ViewManagement.ApplicationViewTitleBar titleBar = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().TitleBar;
public MainPage()
{
this.InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
titleBar.BackgroundColor = Windows.UI.Colors.White;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
titleBar.BackgroundColor = Windows.UI.Colors.Black;
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
//Title bar must be free to change its own colour accord/when/same time Windows theme change
//How can I do that?
//Where the code/methods goes?
}
}
}
You could handle the ColorValuesChanged Event of the UISettings Class .您可以处理 UISettings Class 的ColorValuesChanged事件。 This event will happen when the Windows theme color is changed.
更改 Windows 主题颜色时将发生此事件。 You could get the current color of the Windows theme using
UISettings.GetColorValue()
to check if it is in Dark mode or Light mode.您可以使用
UISettings.GetColorValue()
获取 Windows 主题的当前颜色,以检查它是处于深色模式还是浅色模式。 Then you could change the title bar color as you want.然后,您可以根据需要更改标题栏颜色。
Here is the code:这是代码:
public Windows.UI.ViewManagement.UISettings uiSettings { get; set; }
private void Button_Click(object sender, RoutedEventArgs e)
{
uiSettings = new Windows.UI.ViewManagement.UISettings();
uiSettings.ColorValuesChanged += UiSettings_ColorValuesChanged;
// if you want to stop this.
//uiSettings.ColorValuesChanged -= UiSettings_ColorValuesChanged;
}
private void UiSettings_ColorValuesChanged(Windows.UI.ViewManagement.UISettings sender, object args)
{
// happens when the windows theme is changed.
// The color you get is either black(#FF000000) for dark theme or white(#FFFFFFFF) for light theme.
Color backgroundcolor = uiSettings.GetColorValue(Windows.UI.ViewManagement.UIColorType.Background);
// change titlebar color.
Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().TitleBar.BackgroundColor = backgroundcolor;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.