簡體   English   中英

如何更改 MAUI 中的顏色標題欄?

[英]How to change color title bar in MAUI?

我想像主題顏色一樣更改標題欄顏色。

標題欄

我得到了主題顏色,但我不知道如何更改顏色。 我試試

#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: '找不到元素。 對不起,我的英語不好。

根據官方文檔關於Title bar customization in the 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 中自定義標題欄的問題 它可能會為您提供更多的想法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM