How to Fix unhandled exception in windows when closing a secondary window using the close button in the title bar

Ramin Khalili 60 Reputation points
2023-10-03T08:17:08.5566667+00:00

When Multiple windows are opened using
Application.Current.OpenWindow()
Closing a window (not the primary application window) using the close button in the title bar (specially when the window is not in the foreground) raises an unhandled Exception.

Everything works fine when the windows are closed from within the code using
Application.Current.CloseWindow()

I Have reported the bug with details in :
https://github.com/dotnet/maui/issues/17787

Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,823 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
4,078 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Ramin Khalili 60 Reputation points
    2023-10-10T08:43:42.62+00:00

    Although the issue dose not appear on all windows 11 machines and I don't know why, here is the solution:

    By adding the following lines to the MauiProgram.cs to be able to call the function

    SetBorderAndTitleBar(false, false);

    the problem was fixed. ( Just the lines between #If Windows and #endif.)

    /// Need to use the 2 following name spaces:
    #if WINDOWS 
    using Microsoft.UI;
    using Microsoft.UI.Windowing;
    #endif
    
    namespace MauiAppWindowTest
    {
        public static class MauiProgram
        {
            public static bool NoTitleBar=false;
            public static MauiApp CreateMauiApp()
            {
                var builder = MauiApp.CreateBuilder();
                builder
                    .UseMauiApp<App>()
                    .ConfigureFonts(fonts =>
                    {
                        fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                        fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                    });
    /// The Code below was added
    #if WINDOWS
               builder.ConfigureLifecycleEvents(events =>
               {
                   events.AddWindows(wndLifeCycleBuilder =>
                   { 
                       wndLifeCycleBuilder.OnWindowCreated(window =>
                       {  
                              IntPtr nativeWindowHandle = WinRT.Interop.WindowNative.GetWindowHandle(window);
                              WindowId win32WindowsId = Win32Interop.GetWindowIdFromWindow(nativeWindowHandle);
                              AppWindow appWindow = AppWindow.GetFromWindowId(win32WindowsId);
                              if (appWindow.Presenter is OverlappedPresenter p)
                              {
    							  /// Calling this function resolves the issue
                                  p.SetBorderAndTitleBar(false, false);
                              }
                       });
                   });
               });
    #endif
    /// Up to Here
    
                return builder.Build();
            }
        }
    }
    

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.