Net maui windows platform how to capture the maximized second click event ?

Sami 966 Reputation points
2024-08-09T19:32:59.2666667+00:00

Net maui windows platform how to capture the maximized second click event ? Thanks

Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2024-08-13T05:47:14.5833333+00:00

    Hello,

    If you click the maximized button for the second time. The state is Restored, you can judge the state by adding else if(res == OverlappedPresenterState.Restored) like following code, then send the message as well.

    #if WINDOWS
                   builder.ConfigureLifecycleEvents(events =>
                   {
                    events.AddWindows(windowsLifecycleBuilder =>
                    {
                        windowsLifecycleBuilder.OnWindowCreated(window =>
                        {
                            window.ExtendsContentIntoTitleBar = false;
     
                            IntPtr handle = WinRT.Interop.WindowNative.GetWindowHandle(window);
     
                            var id = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(handle);
     
                            var appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(id);                       
     
                            var p = appWindow.Presenter as OverlappedPresenter;
     
                            var titleBar = appWindow.TitleBar;
     
                             //detailpage events
     
                              appWindow.Changed += (sender, args) => {
     
                        if(args.DidSizeChange)
     
                        {
     
                            OverlappedPresenterState res = p.State;
     
                            if (res == OverlappedPresenterState.Maximized)
     
                            {
    // this will be invoked when maxmized button clicked
     
    WeakReferenceMessenger.Default.Send(new DetailPageStatus("Maximized"));
     
     
                            }
     
                            else if (res == OverlappedPresenterState.Minimized)
     
                            {
    // this will be invoked when Minimized button clicked
     
    WeakReferenceMessenger.Default.Send(new DetailPageStatus("Minimized"));
     
     
                            }
     
                            else if(res == OverlappedPresenterState.Restored) {
     
    // this will be invoked when maxmized button clicked second time, the state is restored.
    
    WeakReferenceMessenger.Default.Send(new DetailPageStatus("Restored"));
     
                                      }
     
                                  }
     
                    };
     
                        });
     
                                                
     
                   });
     
     
        });
    #endif
    

    In you detailedpage, you can get result of the message and handle the Restored state.

    WeakReferenceMessenger.Default.Register<DetailPageStatus>(this, (r, m) =>
    {
         var res = m.Value;
         if (res.Equals("Maximized"))
         {
             if (res != null)
             {
                 gridDetails.IsVisible = true;
             }
             else
             {
                 gridDetails.IsVisible = false;
             }
         }else if (res.Equals("Restored"))
         {
             // this will be invoked when maxmized button clicked for the second time, the state is restored.
     
         }
     
    });
    

    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

1 additional answer

Sort by: Most helpful
  1. youzeliang 895 Reputation points
    2024-08-12T00:11:09.2166667+00:00

    ok,you may see it

    using Microsoft.Maui.Controls;
    using Microsoft.Maui.LifecycleEvents;
    public partial class MainPage : ContentPage
    {
        private bool isFirstMaximize = true;
        public MainPage()
        {
            InitializeComponent();
            Microsoft.Maui.Handlers.WindowHandler.Mapper.AppendToMapping(nameof(IWindow), (handler, view) =>
            {
                if (handler?.PlatformView is Microsoft.UI.Xaml.Window platformWindow)
                {
                    platformWindow.StateChanged += (sender, args) =>
                    {
                        if (platformWindow.WindowState == Microsoft.UI.Windowing.WindowState.Maximized)
                        {
                            if (isFirstMaximize)
                            {
                                isFirstMaximize = false;
                            }
                            else
                            {
                                // Handle the second click when the window is maximized
                                OnSecondMaximizeClick();
                            }
                        }
                        else if (platformWindow.WindowState == Microsoft.UI.Windowing.WindowState.Normal)
                        {
                            isFirstMaximize = true; // Reset when the window is restored to normal
                        }
                    };
                }
            });
        }
        private void OnSecondMaximizeClick()
        {
            // Your logic for handling the second maximize click
        }
    }
    
    
    1. The StateChanged event is triggered whenever the window state changes (e.g., from normal to maximized or minimized).
    2. We track whether the window was maximized previously using the isFirstMaximize flag. If the window is already maximized and the event triggers again, it means a second click occurred while the window is maximized.

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.