简体   繁体   中英

Window doesn't maximize in WinUI3

I am using the following code to start a WinUI3 App maximized:

    public MainWindow()
    {
        this.InitializeComponent();
        var windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(this);
        var windowId = Win32Interop.GetWindowIdFromWindow(windowHandle);
        AppWindow appWindow = AppWindow.GetFromWindowId(windowId);
        OverlappedPresenter presenter = (OverlappedPresenter)appWindow.Presenter;
        presenter.Maximize();
        Debug.WriteLine(presenter.State);
    }

There are no errors, debug output reports the OverlappedPresenter state as Maximized, but the window stays in the restored state.

Any suggestions are welcome, thanks in advance.

Try putting Maximize() in App's OnLaunched method:

    protected override void OnLaunched(LaunchActivatedEventArgs args)
    {
        ...
        presenter.Maximize();
    }

Or the Window's OnActivated event:

    private void OnActivated(object sender, WindowActivatedEventArgs args)
    {
        ...
        presenter.Maximize();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM