如何更改窗口的大小?

问题描述 投票:0回答:1

我从大小为 1285 x 800 的初始内容页面开始。我使用 shell 导航,我说

Shell.Current.Navigation.PushModalAsync(new NewDb());
在 NewDb 内容页面中我有:

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

    Window.Height =  450;
}

在NewDb页面的按钮中我有

Shell.Current.Navigation.PopAsync();
,但是当我返回第一页时,它的高度仍然是450。如何将高度调整为800?

我尝试在第一页的 OnAppearing() 代码隐藏中输入代码,但这似乎不起作用。

c# xaml maui code-behind
1个回答
0
投票

您可以使用以下代码:

创建默认的 MAUI 项目 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();
    }
}

当从MainPage(第一页)导航到NewPage1(NewDb页面)时,高度为450。返回首页时,它也变为800,而不是保持450。

© www.soinside.com 2019 - 2024. All rights reserved.