1

I've tried all of these answers, none of them worked:

  1. https://stackoverflow.com/a/44273903/2901207 -> Simply doesn't do anything, tried to put it in different places but mostly just as the answer does. No response. This code:

    private void MaximizeWindowOnLoad()
    {
        var view = DisplayInformation.GetForCurrentView();
    
        // Get the screen resolution (APIs available from 14393 onward).
        var resolution = new Size(view.ScreenWidthInRawPixels, view.ScreenHeightInRawPixels);
    
        // Calculate the screen size in effective pixels. 
        // Note the height of the Windows Taskbar is ignored here since the app will only be given the maxium available size.
        var scale = view.ResolutionScale == ResolutionScale.Invalid ? 1 : view.RawPixelsPerViewPixel;
        var bounds = new Size(resolution.Width / scale, resolution.Height / scale);
        ApplicationView.GetForCurrentView().SetPreferredMinSize(bounds);
        ApplicationView.PreferredLaunchViewSize = new Size(bounds.Width, bounds.Height);
        ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
    }
    

    I've tried to execute before and after this.InitializeComponent(); of the main page. I've tried to do it in the App constructor but that throws an exception for DisplayInformation.GetForCurrentView();. I have tried to do it just after the Frame is created but no luck as well.

  2. https://stackoverflow.com/a/35250107/2901207 -> Does resize but not maximized, because the window is not in the top-left corner it does not work to remove the -100. Also setting to a very large value returns false for TryResizeView.

  3. And of course I have tried to use: ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.Maximized;

How do I get my UWP app the be maximized but not full screen. I just want to press that maximize button via code! So easy, yet so difficult.

CularBytes
  • 9,924
  • 8
  • 76
  • 101
  • Setting the `Preferred` values sets the behavior for the **next** launch. The current launch has already happened. You can `TryResizeView` to resize what you have now. (I don't see a way to go to Maximized programmatically, though.) – Raymond Chen Oct 31 '19 at 20:01

1 Answers1

0

Everything is fine except you need to do this in App.xaml.cs at OnLaunched event. As @Raymond said in comment you need to add the line below at the end

       Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().TryResizeView(bounds);

The complete code below

App.xaml.cs

    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;
        if (rootFrame == null)
        {
            rootFrame = new Frame();

            rootFrame.NavigationFailed += OnNavigationFailed;
            Window.Current.Content = rootFrame;
        }

        if (e.PrelaunchActivated == false)
        {
            if (rootFrame.Content == null)
            {
                rootFrame.Navigate(typeof(MainPage), e.Arguments);
            }
            MaximizeWindowOnLoad();
            Window.Current.Activate();
        }
    }
    private void MaximizeWindowOnLoad()
    {
        var view = DisplayInformation.GetForCurrentView();
        var resolution = new Size(view.ScreenWidthInRawPixels, view.ScreenHeightInRawPixels);
        var scale = view.ResolutionScale == ResolutionScale.Invalid ? 1 : view.RawPixelsPerViewPixel;
        var bounds = new Size(resolution.Width / scale, resolution.Height / scale);
        ApplicationView.GetForCurrentView().SetPreferredMinSize(bounds);
        ApplicationView.PreferredLaunchViewSize = new Size(bounds.Width, bounds.Height);
        ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
       Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().TryResizeView(bounds);

    }
Vignesh
  • 1,824
  • 2
  • 10
  • 27