Window doesn't maximize in WinUI3
Asked Answered
H

2

1

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.

Hireling answered 5/5, 2022 at 10:12 Comment(0)
U
7

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();
    }
Urgent answered 5/5, 2022 at 10:21 Comment(1)
Using it in OnLaunched, solved my problem. Thanks!Hireling
T
0
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
        Microsoft.UI.WindowId windowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd);
        Microsoft.UI.Windowing.AppWindow appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(windowId);

        if (appWindow != null )
        {
            OverlappedPresenter op = appWindow.Presenter as OverlappedPresenter;
            op.Maximize();
        }
Token answered 24/3, 2023 at 0:34 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Gulp

© 2022 - 2025 — McMap. All rights reserved.