Minimum height and width to maui page

Dani_S 2,826 Reputation points
2023-04-10T08:33:05.9166667+00:00

Hi, I have login and main page. How do i set Minimum height and width to these pages ? Thanks in advance,

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,959 questions
{count} vote

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 26,946 Reputation points Microsoft Vendor
    2023-04-20T09:40:48.9333333+00:00

    Hello,

    For .Net7:
    Page has a content property of the Window class, Window defines the MinimumHeight and MinimumWidth properties, so you can set them in your Page like the following code:

           public partial class LoginPage: ContentPage
    {
       public LoginPage()
        {
            InitializeComponent();
        }
        protected override void OnAppearing()
        {
            base.OnAppearing();
            this.Window.MinimumHeight = 300;
            this.Window.MinimumWidth = 600;
        }
    }
    

    Then you can drag the page to check whether the page can only be dragged to a minimum width of 300(and miniumu Height of 600)

    For .Net6: The Window doesn't have MinimumHeight and MinimumWidth properties, you can try to find the native WinUI Window(AppWindow.Changed event) and set new size when the window size is smaller than the mini size. Please refer to the following code:

    #if WINDOWS
    using Microsoft.UI;
    using Microsoft.UI.Windowing;
    using Windows.Graphics;
    using Windows.UI.WindowManagement;
    #endif
    
    
    protected override void OnAppearing()
        {// in MainPage or LoginPage
            base.OnAppearing();
    #if WINDOWS
            var nativeWindow = this.Window.Handler.PlatformView;
            IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(nativeWindow);
            WindowId windowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(windowHandle);
            Microsoft.UI.Windowing.AppWindow appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(windowId);
            appWindow.Changed += AppWindow_Changed;
    #endif
        }
    
    
    
       private static void AppWindow_Changed(Microsoft.UI.Windowing.AppWindow sender, Microsoft.UI.Windowing.AppWindowChangedEventArgs args)
        {
            var miniWidth = 300;
            var miniHeight = 300;// set different value in  MainPage or in LoginPage
           var oldWindowHeight = sender.Size.Height;
            var oldWindowWidth = sender.Size.Width;
            var newWindowHieght = oldWindowHeight > miniHeight ? oldWindowHeight : miniHeight;
            var newWindowWidth = oldWindowWidth > miniWidth ? oldWindowWidth : miniWidth;
            sender.Resize(new SizeInt32(newWindowWidth, newWindowHieght));
        }
    
    

    Best Regards,

    Wenyan Zhang


    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful