I cannot resize a new window

Eduardo Gomez Romero 845 Reputation points
2024-03-23T17:10:49.36+00:00

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


User's image

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);
     }
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,594 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 44,011 Reputation points Microsoft Vendor
    2024-03-25T02:05:31.98+00:00

    Hello,

    For resizing the created Windows, you should assign values to the width and height properties of the window when you create it.

    Please refer to the following code snippet:

    var newWindow = new Window()
    {
        Page = page,
    
        Height = 200,
    
        Width = 300,
    };
    

    Best Regards,

    Alec Liu.


    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.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.