13

Is there a way (either C# or XAML) I can maximize a UWP app window even after I resized and closed it previously on desktop?

I have tried with ApplicationViewWindowingMode.FullScreen but this makes the app go entire full screen and covers the Windows Taskbar too.

ruffin
  • 16,507
  • 9
  • 88
  • 138
Jessica
  • 2,057
  • 1
  • 15
  • 24

5 Answers5

12

You can use another value PreferredLaunchViewSize from ApplicationViewWindowingMode and then set ApplicationView.PreferredLaunchViewSize but the key is to find out what the size is going to be.

Theoretically, you could use a really big number and window would just extend to the max it could be. However, it's probably safer to just calculate the screen dimensions in effective pixels.

So if you just call the following method before InitializeComponent(); on your main Page, it should maximize the window on startup.

private static void MaximizeWindowOnLoad()
{
    // Get how big the window can be in epx.
    var bounds = ApplicationView.GetForCurrentView().VisibleBounds;

    ApplicationView.PreferredLaunchViewSize = new Size(bounds.Width, bounds.Height);
    ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
}

Note the app somehow remembers these settings even after you uninstalled it. If you ever want to change back to the default behavior (app starts up with the previous window size), simply call ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.Auto; once and remove all the code.

Update

Looks like in the latest Windows 10 build, ApplicationView.GetForCurrentView().VisibleBounds no longer returns the full window size in effective pixels anymore. So we now need a new way to calculate it.

Turns out it's quite straightforward since the DisplayInformation class also gives us the screen resolution as well as the scale factor.

The following is the updated code -

public MainPage()
{
    MaximizeWindowOnLoad();

    InitializeComponent();

    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.PreferredLaunchViewSize = new Size(bounds.Width, bounds.Height);
        ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
    }
} 
Justin XL
  • 38,763
  • 7
  • 88
  • 133
  • Two problems why this is not working for me: (1) This is only applied the next time the application is run. Not the actual execution during which it is set. I notice that the application window is created even before the `App` constructor is called. (2) `VisibleBounds` simply returns the size of the window. (Using `Microsoft.NETCore.UniversalWindowsPlatform` v6.0.1) – Steven Jeuris Jan 07 '18 at 18:05
  • 2
    @StevenJeuris what do you mean by (2)? How would you maximise it without knowing the size of the window? Regarding (1), if it doesn't work for you in your scenario, try to work out a way and improve the answer, downvoting it helps no one. – Justin XL Jan 07 '18 at 22:40
  • @JustinXL By (2) [I mean that `VisibleBounds` returns "the visible region of the window (app view)"](https://learn.microsoft.com/en-us/uwp/api/windows.ui.viewmanagement.applicationview), not the screen size, i.e., not 'how big the window can be'. I tried making it work (and might in the future), at which point I would either edit or add my own answer, but for now I just left a comment providing additional information (feel free to include this in your answer in case you believe this to be correct). – Steven Jeuris Jan 08 '18 at 10:04
  • @StevenJeuris looks like the `VisibleBounds` doesn't return the full window size in epx anymore. See my updated answer. – Justin XL Jan 08 '18 at 12:26
  • Thank you for the update! (removed -1). However, point (1) I made remains quite an important note (at first I thought the `PreferredLaunchViewSize` option had no effect whatsoever). Possibly also a change they made in the latest Windows 10 build. Because of this, I still can't use this answer (hence no +1). – Steven Jeuris Jan 08 '18 at 12:53
  • @StevenJeuris hmm I tried creating a new project, pasted the code in and it ran correctly for the first time. Maybe try calling it at the beginning of the OnLaunch method in App.xaml.cs? – Justin XL Jan 08 '18 at 13:03
  • 1
    I had to call MaximizeWindowOnLoad(); after InitializeComponent(); – Amir Hajiha Jan 29 '19 at 11:03
  • In my case, the updated `MaximizeWindowOnLoad` works the next time the application is opened. The last assigned `PreferredLaunchViewSize` is considered when the application opens, before the control reaches the application code. I am on Windows version 1809. Technically, `ApplicationViewWindowingMode.Maximized` should work, but it doesn't. – MatrixRonny Jun 26 '19 at 14:41
4

If you want to MAXIMISE your app on launch you can use the following:

ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.Maximized;

But be sure to put it into the Loaded Event for your Page or it will not work!

Borisonekenobi
  • 469
  • 4
  • 15
1

I've too few points to comment directly. None of the above resized to a maximized view for me (or the below single-line ApplicationViewWindowingMode.Maximized method), but I have used some of the answers to come up with something that worked for me. It is still very clunky however. The screen size given in 'DisplayInformation' is too big to allow the page to be resized directly to it. Trying to do it didn't work and I had to take 60 off height and width to get it to return 'true', therefore I have the following bit of nonsense which worked, maybe it will help someone else find a better answer. It goes in the page/window loaded event. Nothing else needs to be added elsewhere.

        private void Page_Loaded(object sender, RoutedEventArgs e)
    {
        var view = ApplicationView.GetForCurrentView();
        var displayInfo = DisplayInformation.GetForCurrentView();
        double x = ActualWidth;
        double y = ActualHeight;
        bool answer = true;

        // Get the screen resolution (APIs available from 14393 onward).
        var resolution = new Size(displayInfo.ScreenWidthInRawPixels-60, displayInfo.ScreenHeightInRawPixels-60);

        answer = view.TryResizeView(resolution); //This should return true if the resize is successful
        if (answer)
        {
            x = displayInfo.ScreenWidthInRawPixels - 60;
            y = displayInfo.ScreenHeightInRawPixels - 60;
        }
        answer = true;
        while (answer == true)
        {
            x++;
            answer = view.TryResizeView(new Size { Width = x, Height = y });
        }
        x = x - 1;
        answer = true;
        while (answer == true)
        {
            y++;
            answer = view.TryResizeView(new Size { Width = x, Height = y });
        }
unstuck
  • 27
  • 3
0

Adding the following line to the OnLaunched event under App.xaml.cs did it for me.

ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.FullScreen;

NOTE: Make sure to add it before the following line

 Window.Current.Activate();

If you like to go fullscreen at the runtime use the following line.

ApplicationView.GetForCurrentView().TryEnterFullScreenMode();
SurenSaluka
  • 1,534
  • 3
  • 18
  • 36
0

I have this one liner that works as I expected Justins code to, but for some reason, when using Justins answer, my window would not be maximized... But then I changed something that did make it maximized but I lost all my fluent design such as Acrylic and RevealHighlite...

So I came up with this one liner which keeps all of my fluent design principles happy:

ApplicationView.GetForCurrentView().TryEnterFullScreenMode();

Something to note:

I did try Justins answer, and I am using his method of MaximizeWindowOnLoad() which I have called straight after the initializeComponent();

Full overview:

public class()
        {     
            this.InitializeComponent();
            MaximizeWindowOnLoad();   
        }

private static void MaximizeWindowOnLoad()
        { 
           ApplicationView.GetForCurrentView().TryEnterFullScreenMode();
        }
James Heffer
  • 686
  • 1
  • 6
  • 17
  • Jessica, this method should resolve your window covering the task bar, added benefit, your Acrylic style will remain xD – James Heffer Jun 13 '18 at 11:10