简体   繁体   中英

How to ensure UWP app is always full screen on launch?

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.

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;
    }
} 

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!

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 });
        }

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();

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();
        }

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