Home > database >  how to have the color of the windows theme MAUI
how to have the color of the windows theme MAUI

Time:05-24

I want the main button to be of a color derived from the windows theme. for example if the windows theme is red my buttons are the same color Thank you

CodePudding user response:

UISettings Class could be used to get current system color in windows platform , the point is how to convert Windows.UI.Color to Microsoft.Maui.Graphics.Color .

Sample code

#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

And if you want to change the color dynamically according to system theme, take a look at Application.Current.RequestedThemeChanged event ,it is used to to detect the change of system theme .

Notice: The following event is only triggered when switching between light/dark theme .

Sample code

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
};
  • Related