Net Maui Platform Windows and Mac Desktop how to access from maui

Sami 966 Reputation points
2024-01-27T13:39:38.51+00:00

I have multiple colors of theme on windows and mac How can I access and change colors from maui for setting colors according to theme ? Thanks

<maui:MauiWinUIApplication
    x:Class="XXXDesktop.WinUI.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:maui="using:Microsoft.Maui"
    xmlns:local="using:XXXDesktop.WinUI">
    
    <maui:MauiWinUIApplication.Resources>

        <SolidColorBrush x:Key="ActualWinUITitleBarBrush" Color="#333333" />
        
        
        <StaticResource x:Key="WinUITitleBarBrush" ResourceKey="ActualWinUITitleBarBrush" />
        
        <DataTemplate x:Key="MauiAppTitleBarTemplate">
            <Grid Canvas.ZIndex="1" Height="32" Background="{ThemeResource WinUITitleBarBrush}" VerticalAlignment="Stretch">
                <TextBlock x:Name="AppTitle"  Text="xxx.com"  TextTrimming="CharacterEllipsis" VerticalAlignment="Center" Margin="10,0,0,2"/>
            </Grid>
        </DataTemplate>
    </maui:MauiWinUIApplication.Resources>
</maui:MauiWinUIApplication>
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
4,026 questions
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 81,016 Reputation points Microsoft External Staff
    2024-01-31T09:19:46.45+00:00

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.