我希望主按鈕的顏色源自 Windows 主題。例如,如果 windows 主題是紅色的,我的按鈕是相同的顏色 謝謝
uj5u.com熱心網友回復:
UISettings 類可以用來獲取windows平臺的當前系統顏色,關鍵是如何轉換Windows.UI.Color為Microsoft.Maui.Graphics.Color.
示例代碼
#if WINDOWS
var uiSettings = new Windows.UI.ViewManagement.UISettings();
var color = uiSettings.GetColorValue(UIColorType.Accent);
//change button color with system theme color
button.BackgroundColor = Microsoft.Maui.Graphics.Color.Parse(color.ToString());
#endif
如果你想根據系統主題動態改變顏色,看看Application.Current.RequestedThemeChanged事件,它用于檢測系統主題的變化。
注意:以下事件僅在淺色/深色主題切換時觸發。
示例代碼
Application.Current.RequestedThemeChanged = (s, a) =>
{
#if WINDOWS
var uiSettings = new Windows.UI.ViewManagement.UISettings();
var color = uiSettings.GetColorValue(UIColorType.Accent);
//change button color with system theme color
button.BackgroundColor = Microsoft.Maui.Graphics.Color.Parse(color.ToString());
#endif
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/481619.html
標籤:C# xml 虚拟机 毛伊岛 maui-windows
