maui -Minimum width and minimum height in content page

Dani_S 4,501 Reputation points
2023-10-17T15:20:42.1666667+00:00

Hi,

i used net 7 + vs 17.4

in main page i used and got null reference on window

protected override void OnAppearing()
    {
        base.OnAppearing();
        this.Window.MinimumHeight = 300;
        this.Window.MinimumWidth = 600;
    }


first i login using app shell as follow:

public App()
        {
            InitializeComponent();

            MainPage =  new AppShell();


<?xml version="1.0" encoding="UTF-8" ?>
<Shell
    x:Class="GssdDesktopClient.Maui.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:GssdDesktopClient.Maui.Pages"
    Shell.FlyoutBehavior="Disabled">
    <ShellContent
        ContentTemplate="{DataTemplate local:LoginPage}"
        Route="LoginPage" />
        
</Shell>

Later on login button click i move to main page and there i put :

await Application.Current.MainPage.Navigation.PushAsync(new MainPage(), true);

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="GssdDesktopClient.Maui.Pages.MainPage"
             xmlns:telerik="http://schemas.telerik.com/2022/xaml/maui"
             xmlns:CustomControls ="clr-namespace:GssdDesktopClient.Maui.CustomControls"
       Shell.NavBarIsVisible="False">
    <Shell.BackButtonBehavior>
        <BackButtonBehavior IsVisible="False" />
    </Shell.BackButtonBehavior> 


but this code on main page n

protected override void OnAppearing()
        {
            base.OnAppearing();
           this.Window.MinimumHeight = 600;
           this.Window.MinimumWidth = 600;
        }

ot working:

Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-10-18T03:21:00.38+00:00

    Hello,

    I can reproduce your issue, you can create a window in the app.xaml.cs, then set window to static, then you can change the MinimumHeight and MinimumWidth in the MainPage's OnAppearing method.

    public partial class App : Application
      {
          public App()
          {
              InitializeComponent();
              //remove following code
              //  MainPage = new AppShell();
          }
          public static Window window;
          protected override Window CreateWindow(IActivationState activationState)
          {
             window = new Window(new AppShell());   
              return window;
          }
     }
    

    Then you can set it in the MainPage's OnAppearing method.

    protected override void OnAppearing()
      {
          base.OnAppearing();
            App.window.MinimumWidth = 600;
            App.window.MinimumHeight = 600;
    
     }
    

    Best Regards,

    Leon Lu


    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.

    2 people found this answer helpful.
    0 comments No comments

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.