mauimaui-blazormaui-windows

Open in full screen page only on first Home/ Login Page in Maui hybrid application


I am creating one application, my requirement is to open the first page of the application in full screen with restrict drag, resize and double tap of side bar feature. When navigating from another page it should open as normal with all default features.

I tried to do this in App.xaml.cs page inside Platform->Windows->App.xaml.cs page:

      public partial class App : MauiWinUIApplication
  {
      public static bool IsLoginPageActive { get; set; } = false;

      public App()
      {
          this.InitializeComponent();
          Microsoft.Maui.Handlers.WindowHandler.Mapper.AppendToMapping(nameof(IWindow), (handler, view) =>
          {
              var mauiWindow = handler.VirtualView;
              var nativeWindow = handler.PlatformView;
              nativeWindow.Activate();

              nativeWindow.ExtendsContentIntoTitleBar = false;

              IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(nativeWindow);
              WindowId windowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(windowHandle);
              AppWindow appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(windowId);

              if (Microsoft.Maui.IPlatformApplication.Current is App app)
              {
                  if (MauiHybridWindow.App.IsMainPageActive == true)
                  {
                      ApplyLoginPageWindowSettings(appWindow);
                  }
                  else
                  {
                      ResetWindowSettings(appWindow);
                  }
              }
          });

      }

      protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
      private void ApplyLoginPageWindowSettings(AppWindow appWindow)
      {
          if (appWindow.Presenter is OverlappedPresenter p)
          {
              p.IsResizable = false;
              p.IsMaximizable = false;
              p.IsMinimizable = false;
              p.SetBorderAndTitleBar(true, true);
              p.Maximize();

          }
      }

      private void ResetWindowSettings(AppWindow appWindow)
      {
          if (appWindow.Presenter is OverlappedPresenter p)
          {
              p.IsResizable = true;
              p.IsMaximizable = true; 
              p.IsMinimizable = true; 
              p.Maximize();
          }
      }

  }

Then from All platform App.xaml.cs did this:

  namespace MauiHybridWindow
{
   public partial class App : Application
   {
       public static bool IsMainPageActive { get; set; } = true;
       public App()
       {
           InitializeComponent();
           if (IsMainPageActive)
           {
               MainPage = new MainPage();
           }
       }

       public void OnChangeMainPage(bool isOtherPage)
       {
           IsMainPageActive = isOtherPage;
           new App();
       }
   }
}

How can i achieve mine desired result while navigating to another page ?


Solution

  • I have tested your code and it will run the application as full screen defaultly. But I didn't see you used OnChangeMainPage method any where.

    How can i achieve mine desired result while navigating to another page ?

    Generally speaking, you can call the native code about resetting window settings when you navigated.

    If navigating between razor page: you can refer to the code about the home.razor below.

    @page "/"
    @inject NavigationManager nv;
    <h1>Hello, world!</h1>
    
    Welcome to your new app.
    
    @code{
        protected override void OnInitialized()
        {
            base.OnInitialized();
            nv.LocationChanged += (o, e) =>
            {
                #if WINDOWS
                var window = App.Current.MainPage.Window.Handler.PlatformView as Microsoft.UI.Xaml.Window;
                var windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(window);
                var windowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(windowHandle);
                var appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(windowId);
                if (appWindow.Presenter is Microsoft.UI.Windowing.OverlappedPresenter p)
                {
                    p.IsResizable = true;
                    p.IsMaximizable = true;
                    p.IsMinimizable = true;
                    p.Maximize();
            }
                #endif
            };
        }
    }