如何更改毛伊岛的标题栏颜色?



我想把标题栏的颜色改成主题色。

标题栏

我得到主题颜色,但我不知道如何改变颜色。我试着

#if WINDOWS
var uiSettings = new Windows.UI.ViewManagement.UISettings();
var color = uiSettings.GetColorValue(UIColorType.Accent);
var appView = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView();
var titleBar = appView.TitleBar;
titleBar.BackgroundColor = color;
#endif

我得到异常System.Runtime.InteropServices.COMException: '元素未找到。对不起,我的英语不好。

根据WinUI3中关于标题栏定制的官方文档,你应该使用WinUI3的api来完成。您使用的代码是用于uwp的,而不是用于winui3的。

所以你可以试试下面的代码:
#if WINDOWS
            var uiSettings = new Windows.UI.ViewManagement.UISettings();
            var color = uiSettings.GetColorValue(UIColorType.Accent);
            Microsoft.UI.Xaml.Window window = (Microsoft.UI.Xaml.Window)App.Current.Windows.First<Window>().Handler.PlatformView;
//get the current window on the windows platform
            IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(window);
            Microsoft.UI.WindowId windowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(windowHandle);
            Microsoft.UI.Windowing.AppWindow appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(windowId);
            Microsoft.UI.Windowing.AppWindowTitleBar titlebar = appWindow.TitleBar;
           //titlebar.ExtendsContentIntoTitleBar = true;
// in the official document, this line is needed, but when I used it, the background color didn't change
            titlebar.BackgroundColor = color;
#endif

我提到了关于在maui中定制标题栏的问题。它可能给你提供更多的想法。

最新更新