Hello,
You can do this by accessing the SolidColorBrush in the background code, then change the color in the Platforms/Windows/App.xaml.cs
. Next, you can invoke this change color code by Messenger - Community Toolkits for .NET | Microsoft Learn.
Firstly, please create a Messager (please install the CommunityToolkit.Mvvm nuget package, before you use it)
// Create a message
public class ChangedThemeColorMessage : ValueChangedMessage<byte[]>
{
public ChangedThemeColorMessage(byte[] ColorBytes) : base(ColorBytes)
{
}
}
Then you can send the message with byte[], byte[] contains color byte. I used button click event for testing.
private void OnCounterClicked(object sender, EventArgs e)
{
byte[] bytes = { 255, 255, 255, 0 };
WeakReferenceMessenger.Default.Send(new ChangedThemeColorMessage(bytes));
}
In the end, open your Platforms/Windows/App.xaml.cs
and find CreateMauiApp
method, you can get the SolidColorBrush by key and change the color from transferred byte[].
protected override MauiApp CreateMauiApp()
{
WeakReferenceMessenger.Default.Register<ChangedThemeColorMessage>(this, (r, m) =>
{
byte[] Colors= m.Value as byte[];
if (Colors!=null)
{
if (MauiWinUIApplication.Current.Resources.ContainsKey("ActualWinUITitleBarBrush"))
{
Microsoft.UI.Xaml.Media.SolidColorBrush brush = MauiWinUIApplication.Current.Resources["ActualWinUITitleBarBrush"] as Microsoft.UI.Xaml.Media.SolidColorBrush;
if (brush != null)
{
brush.Color = Windows.UI.Color.FromArgb(Colors[0], Colors[1], Colors[2], Colors[3]);
}
}
}
});
return MauiProgram.CreateMauiApp();
}
Best Regards, Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.