net maui windows platform when maximaze or minimize the page I want to hide and show contents ?

Sami 901 Reputation points
2024-07-23T00:58:52.5833333+00:00

net maui windows platform when maximaze or minimize the page I want to hide and show contents ? when my listing s detailpage opened small size how can i create events for my seconwindow page. maximize show some contents and then minimize back again default page. Thanks

```javascript

#if WINDOWS

using Microsoft.UI;

using Microsoft.UI.Windowing;

using Microsoft.Maui.LifecycleEvents;

using Windows.Graphics;

using static Microsoft.Maui.ApplicationModel.Permissions;

using Colors = Microsoft.Maui.Graphics.Colors;

using CitysDesktop.Helpers;

using CitysDesktop.Views.PartialViews;

#endif

using Mopups.Hosting;

using Sharpnado.Tabs;

namespace CitysDesktop

{


public static class MauiProgram

{

    public static MauiApp CreateMauiApp()

    {

        var builder = MauiApp.CreateBuilder();

        builder

            .UseMauiApp<App>()

            .UseMauiCommunityToolkit()

            .UseMauiCommunityToolkitCore()

            .UseMauiCommunityToolkitMediaElement()

            .UseSharpnadoTabs(loggerEnable: false)

            .ConfigureMopups()

            .ConfigureFonts(fonts =>

            {

                fonts.AddFont("Ubuntu-R.ttf", "Ubuntu");

                fonts.AddFont("digital-7.ttf", "Digital");

                fonts.AddFont("Lato-Black.ttf", "Lato");

                fonts.AddFont("Poppins-Regular.otf", "AppFontFamily");

                fonts.AddFont("Poppins-Medium.otf", "AppMediumFontFamily");

                fonts.AddFont("Poppins-SemiBold.otf", "AppSemiBoldFontFamily");

                fonts.AddFont("Poppins-Bold.otf", "AppBoldFontFamily");

                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");

                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");

                fonts.AddFont("Font-Awesome-Solid.otf", "FontAwesomeSolid");

                fonts.AddFont("fa-solid-900.ttf", "FaPro");

                fonts.AddFont("fa-brands-400.ttf", "FaBrands");

                fonts.AddFont("fa-regular-400.ttf", "FaRegular");

                fonts.AddFont("line-awesome.ttf", "LineAwesome");

                fonts.AddFont("material-icons-outlined-regular.otf", "MaterialDesign");

                fonts.AddFont("ionicons.ttf", "IonIcons");

                fonts.AddFont("icon.ttf", "MauiKitIcons");

                fonts.AddFont("fontello.ttf", "Fontello");

            });
#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;

                          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 { 

                        }

                    }

                };

                         var foregroundColor = new Windows.UI.Color()

                        { 

                            A = 20,

                            R = 255,

                            G = 255,

                            B = 255,

                        };


       

                        ////titleBar.BackgroundColor = favoriteColor;

                        ////titleBar.ButtonBackgroundColor = favoriteColor;

                        ////titleBar.ButtonForegroundColor = favoriteColor;

                        ////titleBar.InactiveBackgroundColor = favoriteColor;

                        ////titleBar.InactiveForegroundColor = Colors.White.ToWindowsColor();

                        ////titleBar.ButtonInactiveBackgroundColor = favoriteColor;

                        ////titleBar.ButtonInactiveForegroundColor = Colors.White.ToWindowsColor();

                        //titleBar.ButtonForegroundColor = foregroundColor;

                        //////titleBar.ButtonPressedBackgroundColor = Colors.DarkGray.ToWindowsColor();

                        titleBar.ButtonHoverBackgroundColor = foregroundColor;    

                        // //titleBar.ButtonHoverForegroundColor = favoriteColor;

                           //middle of screen

                        if (appWindow is not null)

                                    {

                                        Microsoft.UI.Windowing.DisplayArea displayArea = Microsoft.UI.Windowing.DisplayArea.GetFromWindowId(id, Microsoft.UI.Windowing.DisplayAreaFallback.Nearest);

                                        if (displayArea is not null)

                                        {

                                            var CenteredPosition = appWindow.Position;

                                            CenteredPosition.X = ((displayArea.WorkArea.Width - appWindow.Size.Width) / 2);

                                            CenteredPosition.Y = ((displayArea.WorkArea.Height - appWindow.Size.Height) / 2);

                                            appWindow.Move(CenteredPosition);

                                            //appWindow.Resize(new SizeInt32(WindowWidth, WindowHeight));// used for Resize the window screen UI

                                        }

                                    }

                        //if(appWindow.Presenter is OverlappedPresenter p)

                        //{

                        //    TitleBarHelper.p = p;

                        //    TitleBarHelper.p.SetBorderAndTitleBar(false, false);

                        //    TitleBarHelper.p.IsMaximizable = false;

                        //    TitleBarHelper.p.IsResizable = false;

                        //    TitleBarHelper.p.IsMinimizable = false;


       

                        //}

                                                    //hide titlebar

                                                            //switch (appWindow.Presenter)

                                                            //{

                                                            //    case Microsoft.UI.Windowing.OverlappedPresenter overlappedPresenter:

                                                            //        overlappedPresenter.IsMaximizable = false;

                                                            //        overlappedPresenter.IsResizable = false;

                                                            //        overlappedPresenter.IsMinimizable = false;     

                                                            //        overlappedPresenter.SetBorderAndTitleBar(false, false);

                                                            //        break;

                                                            //}

                    });


                                            

               });



    });
#endif


        builder.ConfigureMauiHandlers(handlers =>

               {

                   handlers.AddHandler<CollectionView, Handlers.CustomCollectionViewHandler>();

               });
#if DEBUG


    builder.Logging.AddDebug();
#endif


        return builder.Build();

    }

    public class DetailPageStatus : ValueChangedMessage<string>

    {

        public DetailPageStatus(string msg) : base(msg)

        {

        }

    }

}
}



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

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 72,251 Reputation points Microsoft Vendor
    2024-07-23T08:01:41.2833333+00:00

    Hello,

    Please open your MauiProgram.cs, You can detect the window size changed by winuiAppWindow.Changed event. Then detect maximaze or minimize page state the by OverlappedPresenterState.

    #if WINDOWS
            builder.ConfigureLifecycleEvents(events =>
            {
                events.AddWindows(wndLifeCycleBuilder =>
                {
                    wndLifeCycleBuilder.OnWindowCreated(window =>
                    {
                        IntPtr nativeWindowHandle = WinRT.Interop.WindowNative.GetWindowHandle(window);
                        WindowId win32WindowsId = Win32Interop.GetWindowIdFromWindow(nativeWindowHandle);
                        AppWindow winuiAppWindow = AppWindow.GetFromWindowId(win32WindowsId);
                        var p = winuiAppWindow.Presenter as OverlappedPresenter;
     
                        winuiAppWindow.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 StatusChangedMessage("Maximized"));
    
                                }
                                else if (res == OverlappedPresenterState.Minimized)
                                {
      // this will be invoked when Minimized button clicked
    
    
    WeakReferenceMessenger.Default.Send(new StatusChangedMessage("Minimized"));
                                }
                                else { 
    
                                }
                            }
                        };
                    });
                });
            });
    #endif
    

    By the way, please do not forget to add these namespace in the MauiProgram.cs

    #if WINDOWS
    using Microsoft.Maui.LifecycleEvents;
    using Microsoft.UI;
    using Microsoft.UI.Windowing;
    #endif
    

    After that, you can send WeakReferenceMessenger from MauiProgram.cs to your specific pages. When winuiAppWindow.Changed get maximaze or minimize the page event, you can hide these controls when you get messages in detailed page.

    
    // Create a message
    public class StatusChangedMessage : ValueChangedMessage<string>
    {
         public StatusChangedMessage(string msg) : base(msg)
         {
         }
    }
    
    public NewPage1()
    {
    	InitializeComponent();
     
     
        // Register a message in some module
        WeakReferenceMessenger.Default.Register<StatusChangedMessage>(this, (r, m) =>
        {
            var res=m.Value;
            if (res.Equals("Maximized"))
            {
     
            }else if (res.Equals("Minimized"))
            {
     
            }
        });
    }
    

    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.