Net Maui Net 8 Handler is not working as expected ? how to fix ?

Sami 861 Reputation points
2023-11-14T04:46:20.5433333+00:00
 I have handler like as above but only titleBar.backgroundColor is not showing as expected (still showing gray color ) the others working well any idea to fix  ? thanks..

// windows app.css

WindowHandler.Mapper.AppendToMapping(nameof(IWindow), (handler, view) =>
 {
     var width = 1420;
     var height = 838;

     var nativeWindow = handler.PlatformView;
     nativeWindow.Activate();

     var hWnd = WindowNative.GetWindowHandle(nativeWindow);
     var windowId = Win32Interop.GetWindowIdFromWindow(hWnd);
     var appWindow = AppWindow.GetFromWindowId(windowId);

     var titleBar = appWindow.TitleBar;

     // Set active window colors
          
     titleBar.ForegroundColor = Colors.White.ToWindowsColor();
     titleBar.BackgroundColor =   Colors.Black.ToWindowsColor();
     titleBar.ButtonForegroundColor = Colors.White.ToWindowsColor();
     titleBar.ButtonBackgroundColor = Colors.Black.ToWindowsColor();
     titleBar.ButtonHoverForegroundColor = Colors.White.ToWindowsColor();
     titleBar.ButtonHoverBackgroundColor = Colors.Orange.ToWindowsColor();
     titleBar.ButtonPressedForegroundColor = Colors.Black.ToWindowsColor();
     titleBar.ButtonPressedBackgroundColor = Colors.Orange.ToWindowsColor();

     // Set inactive window colors
     titleBar.InactiveForegroundColor = Colors.White.ToWindowsColor();
     titleBar.InactiveBackgroundColor = Colors.Black.ToWindowsColor();
     titleBar.ButtonInactiveForegroundColor = Colors.White.ToWindowsColor();
     titleBar.ButtonInactiveBackgroundColor = Colors.Black.ToWindowsColor();
 });
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,898 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,656 Reputation points Microsoft Vendor
    2023-11-14T08:25:41.9633333+00:00

    Hello,

    You can move code about setting active/inactive window colors to the ConfigureLifecycleEvents method of MauiProgram.cs.

     var builder = MauiApp.CreateBuilder();
                builder
                    .UseMauiApp<App>()
                    .ConfigureLifecycleEvents(events =>
                    {
    #if WINDOWS
                        events.AddWindows(windowsLifecycleBuilder =>
                        {
                            windowsLifecycleBuilder.OnWindowCreated(window =>
                            {              
                                var handle = WinRT.Interop.WindowNative.GetWindowHandle(window);
                                var id = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(handle);
                                var appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(id);
                               
                                var titleBar = appWindow.TitleBar;
                                // Set active window colors
    ...
    
                               // Set inactive window colors
    ...                           
    
                           });
    #endif                      
                       });
                    })
    

    By the way, if you think you've found a bug in Maui please report it in the Maui GitHub repository at https://github.com/dotnet/maui . We will clear details to be able to reproduce and troubleshoot the problem. When you post it, please include version numbers, exact repro steps, and the difference between the expected behavior and actual behavior.

    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.