简体   繁体   English

当 Windows 10 从深色主题更改为浅色时,如何更改 uwp 应用程序标题栏颜色,反之亦然?

[英]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 个按钮:

  • one for change title bar color to white一种用于将标题栏颜色更改为白色
  • one for change title bar color to black一种用于将标题栏颜色更改为黑色
  • one for let the app title bar free to follow the Windows 10 theme color for its app title bar一种让应用程序标题栏自由跟随其应用程序标题栏的 Windows 10 主题颜色

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.

相关问题 在Windows Phone中将用户输入上的应用程序主题从暗更改为亮,反之亦然 - Change app theme on user input from dark to light and vice versa in Windows Phone 用于windows的Eclipse黑暗主题:如何将滚动条和标题栏的颜色更改为黑暗? - Eclipse dark theme for windows: how to change the color of scroll bars and title bars to dark? 如何更改颜色以调整 UWP 中的主题设置? - How to change color to adjust theme setting in UWP? Windows 10 UAP更改颜色主题 - Windows 10 UAP change color theme 每当我在UWP中将选项从“是”更改为“否”时,如何从列表中替换选中的单选按钮的值? - How can i replace the value of checked radio button from a LIST whenever i change my option from yes to no vice versa in UWP 在 flutter windows 桌面更改标题栏颜色 - Change title bar color in flutter windows desktop #UWP获取系统主题(浅色/深色) - #UWP get system theme (Light/Dark) 如何以编程方式更改 Win 8.1 或 Win 10 UWP 应用程序的背景主题? - How to programmatically change background theme of Win 8.1 or Win 10 UWP app? Python - 如何更改 windows 颜色主题 - Python - How to change windows color theme 如何让我的表单标题栏遵循 windows 深色主题? - How do I make my Form title bar follow the windows dark theme?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM