user22995906
user22995906

Reputation:

How do I change the size of a window?

I start with an initial content page with a size of 1285 x 800. I'm using shell navigation and I say Shell.Current.Navigation.PushModalAsync(new NewDb()); and in the NewDb content page I have:

protected override void OnAppearing()
{
    base.OnAppearing();

    Window.Height =  450;
}

In a button of the NewDb page I have Shell.Current.Navigation.PopAsync(); but when I return back to the first page, it remains the height of 450. How do I resize the height to 800?

I tried entering code in the OnAppearing() code-behind in the first page but that doesn't seem to work.

Upvotes: 0

Views: 132

Answers (1)

Jianwei Sun - MSFT
Jianwei Sun - MSFT

Reputation: 4282

You can use the following code:

Create a default MAUI project, MainPage.xaml:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiApp1.MainPage">

    <VerticalStackLayout>
        <Button Text="go" Clicked="Button_Clicked"/>
    </VerticalStackLayout>

</ContentPage>

MainPage.xaml.cs:

namespace MauiApp1
{
    public partial class MainPage : ContentPage
    {
       public MainPage()
        {
            InitializeComponent();
        }

       private void Button_Clicked(object sender, EventArgs e)
        {
            Shell.Current.Navigation.PushModalAsync(new NewPage1());
        }

       protected override void OnAppearing()
        {
            base.OnAppearing();
            Window.Height = 800;
        }
   }
}

NewPage1.xaml:

<ContentPage ...
             x:Class="MauiApp1.NewPage1"
             Title="NewPage1">

    <VerticalStackLayout>
        <Label
            Text="Welcome to .NET MAUI!"
            VerticalOptions="Center"
            HorizontalOptions="Center" />        

        <Button Text="back" Clicked="Button_Clicked"/>

    </VerticalStackLayout>
</ContentPage>

NewPage1.xaml.cs:

public partial class NewPage1 : ContentPage
{
    public NewPage1()
    {
        InitializeComponent();
    }
    protected override void OnAppearing()
    {
        base.OnAppearing();
        Window.Height = 450;
    }

   private void Button_Clicked(object sender, EventArgs e)
    {
        Shell.Current.Navigation.PopAsync();
    }
}

When navigate from MainPage(first page) to NewPage1(NewDb page), it will be the height of 450. And return back to the first page, it also change to 800, not remains 450.

Upvotes: 0

Related Questions