I have a Main page with two buttons
<Grid RowDefinitions="200, *">
<HorizontalStackLayout>
<Button
x:Name="page1Btn"
Clicked="page1Btn_Clicked"
Text="Open page 1" />
<Button
x:Name="page2Btn"
Clicked="page2Btn_Clicked"
Text="Open Page 2" />
</HorizontalStackLayout>
</Grid>
private void page2Btn_Clicked(object sender, EventArgs e) {
WindowHelper.OpenWindow(new Page1 { MinimumWidthRequest = 100, MinimumHeightRequest = 100 });
}
private void page1Btn_Clicked(object sender, EventArgs e) {
Application.Current?.OpenWindow(new Window { Page = new Page_2() });
}
public static class WindowHelper {
public static void OpenWindow(Page page) {
if (page != null) {
page.MinimumHeightRequest = 50;
page.MinimumWidthRequest = 50;
page.MaximumHeightRequest = 50;
page.MaximumWidthRequest = 50;
MainThread.BeginInvokeOnMainThread(() => {
page.MinimumWidthRequest = 0;
page.MinimumHeightRequest = 0;
page.MaximumWidthRequest = double.PositiveInfinity;
page.MaximumHeightRequest = double.PositiveInfinity;
});
var newWindow = new Window() {
Page = page
};
App.Current?.OpenWindow(newWindow);
}
I need a Window to be smaller, and not just the page.
I am reading the docs, and it doesn't work.
Position and size a Window
The position and size of a window can be programmatically defined for a .NET MAUI app on Windows by setting the X, Y, Width, and Height properties on a Window object.
Warning
Mac Catalyst doesn't support resizing or repositioning windows programmatically by setting the X, Y, Width, and Height properties.
For example, to set the window position and size on launch you should override the CreateWindow method in your App class and set the X, Y, Width, and Height properties on a Window object:
public partial class App : Application
{
public App()
{
InitializeComponent();
}
protected override Window CreateWindow(IActivationState activationState) =>
new Window(new AppShell())
{
Width = 700,
Height = 500,
X = 100,
Y = 100
};
}
Alternatively, a window can be positioned and sized by accessing the Window property from any page, layout, or view. For example, the following code shows how to position a window in the center of the screen:
Mac Catalyst
Mac Catalyst doesn't support resizing or repositioning windows programmatically. However, a workaround to enable resizing is to set the MinimumWidth and MaximumWidth properties to the desired width of the window, and the MinimumHeight and MaximumHeight properties to the desired height of the window. This will trigger a resize, and you can then revert the properties back to their original values:
Window.MinimumWidth = 700;
Window.MaximumWidth = 700;
Window.MinimumHeight = 500;
Window.MaximumHeight = 500;
// Give the Window time to resize
Dispatcher.Dispatch(() =>
{
Window.MinimumWidth = 0;
Window.MinimumHeight = 0;
Window.MaximumWidth = double.PositiveInfinity;
Window.MaximumHeight = double.PositiveInfinity;
});
I am trying to make this work on mac and windows.
I want to be able to create a new window, with a specific size and a full scream.
I tried to make it full scream with this
public static void OpenWindow(Page page) {
if (page != null) {
MainThread.BeginInvokeOnMainThread(() => {
page.WidthRequest = DeviceDisplay.MainDisplayInfo.Width;
page.HeightRequest = DeviceDisplay.MainDisplayInfo.Height;
});
var newWindow = new Window() {
Page = page
};
App.Current?.OpenWindow(newWindow);
}