Maximum UWP application window at startup

Is there a way (C # or XAML) that I can maximize the UWP application window even after I resized and closed it earlier on the desktop?

I tried with ApplicationViewWindowingMode.FullScreen , but this makes the application fully complete and covers the Widnows taskbar.

+5
source share
1 answer

You can use another PreferredLaunchViewSize value from ApplicationViewWindowingMode and then set ApplicationView.PreferredLaunchViewSize , but the key is to figure out what size it will be.

Theoretically, you could use a really large number, and the window would simply expand to the maximum possible. However, it is probably safer to simply calculate screen sizes in effective pixels.

So, if you just call the following method before InitializeComponent(); on its main Page , it should maximize the window at 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; } 

Please note that the application somehow remembers these settings even after deleting it. If you want to return to the default behavior (the application starts with the previous window size), just call ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.Auto; once and delete all the code.

Update

It appears that in the latest version of Windows 10, ApplicationView.GetForCurrentView().VisibleBounds no longer returns the full window size in effective pixels. So now we need a new way to calculate it.

This is quite simple, since the DisplayInformation class also gives us screen resolution as well as a scale factor.

Below 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; } } 
+5
source

Source: https://habr.com/ru/post/1268375/


All Articles