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.