How to Override the Close Button in the Title Bar of a secondary windows in .net Maui

Ramin Khalili 60 Reputation points
2023-10-07T08:44:28.42+00:00

In .Net Maui I have Opened a Secondary window named "MyWin" using :
Application.Current.OpenWindow(MyWin);

On Windows Platform I would like to Override the closing process, caused by pressing the Close Button in the title bar, of this secondary window "MyWin" to show an "Are you sure" message and ignore the closing procedure depending on user response.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,000 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 69,691 Reputation points Microsoft Vendor
    2023-10-09T02:29:12.4733333+00:00

    Hello,

    You can do this by following steps.

    Firstly, please set a title for second windows, we will use this title name to get the second windows in the MauiProgram.cs

    NewPage3 newPage3 =   new NewPage3();
     Window MyWin =  new Window(newPage3);
    
      MyWin.Title = "MyWin";
    
      Application.Current.OpenWindow(MyWin);
    

    Next, please open MauiProgram.cs and add ConfigureLifecycleEvents for builder in the CreateMauiApp method. Based on the title name, we can get the second window, if user click the close button in the titlebar, this appWindow.Closing event will be executed.

    When the user executes the closing event, we can push a display alert. If the user clicks Yes, close this application; if they click the Cancel, the display alert will dismiss. Here is a simple code that you can refer to.

       var builder = MauiApp.CreateBuilder();
                builder.ConfigureLifecycleEvents(events =>
                {
    
    #if WINDOWS
    events.AddWindows(windowsLifecycleBuilder =>
    {
        windowsLifecycleBuilder.OnWindowCreated(window =>
        {
            if (window.Title== "MyWin")
            {
                //get the Secondary window title named "MyWin"
                var windows = window;
                //use Microsoft.UI.Windowing functions for window
                var handle = WinRT.Interop.WindowNative.GetWindowHandle(window);
                var id = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(handle);
                var appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(id);
    
               //When user execute the closing method, we can push a display alert. If user click Yes, close this application, if click the cancel, display alert will dismiss.
                appWindow.Closing += async (s, e) =>
                {
                    e.Cancel = true;
                    //get all of Microsoft.Maui.Controls.windows.
                    var windows1 = Application.Current.Windows.ToList<Microsoft.Maui.Controls.Window>();
                    foreach (Window win in windows1) {
                        //get the Secondary window title named "MyWin"
                        if (win.Title== "MyWin")
                        {                    
                            bool result = await win.Page.DisplayAlert(
                            "Alert title",
                            "You sure want to close app?",
                            "Yes",
                            "Cancel");
                           if (result)
                            {
                                Application.Current.CloseWindow(win);
                            }
                        }
                    }
    
               };
            }
          
        });
    });
    #endif
    
               })
    .UseMauiApp<App>()
    
    

    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.

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful