我希望主按钮的颜色来自windows主题。例如,如果Windows的主题是红色,我的按钮是相同的颜色谢谢你
uissetings类可以用来获取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
#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 . requestedthemechange事件,它用于检测系统主题的变化。
注意:以下事件仅在浅色/深色主题之间切换时触发。
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
};
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
};