Setting window size on desktop for a Windows 10 UWP app
For the very first app launch, the ApplicationView.PreferredLaunchWindowingMode
is set to ApplicationViewWindowingMode.Auto
regardless of what you set in your code.
However, from this question on MSDN, there may be a way to overcome this. One of the answers gives a way to set that very first launch size (reverting to Auto
after that).
If your goal is to launch only once at a
PreferredLaunchViewSize
, you can use this rude solution (up to you for a better implementation with your coding style! :P)public MainPage() { this.InitializeComponent(); var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; if (localSettings.Values["launchedWithPrefSize"] == null) { // first app launch only!! ApplicationView.PreferredLaunchViewSize = new Size(100, 100); ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize; localSettings.Values["launchedWithPrefSize"] = true; } // resetting the auto-resizing -> next launch the system will control the PreferredLaunchViewSize ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.Auto; } }
P.S. I have not tested this.
You don't really have control over the window size, and even if you will try to re-size it it may fail. I've asked the same question on MSDN forums and got the answer here:
Windows 10 universal DirectX application
BTW, here is the solution in your event handler "OnLaunched" or in your Event Handler "OnActivated" find:
Window.Current.Activate();
And replace it with:
float DPI = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi;
Windows.UI.ViewManagement.ApplicationView.PreferredLaunchWindowingMode = Windows.UI.ViewManagement.ApplicationViewWindowingMode.PreferredLaunchViewSize;
var desiredSize = new Windows.Foundation.Size(((float)800 * 96.0f / DPI), ((float)600 * 96.0f / DPI));
Windows.UI.ViewManagement.ApplicationView.PreferredLaunchViewSize = desiredSize;
Window.Current.Activate();
bool result = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().TryResizeView(desiredSize);
It is better if you place this code into the "OnActivated()" event handler as it will set your defined size when the app starts and when it becomes active after any interruptions.
In the "desiredSize" calculation, 800 is the width and 600 is the height. This calculation is needed, because the size is in DPI, so you have to convert it from pixels to DPI.
Also keep in mind that size cannot be smaller than "320x200".
Try setting PreferredLaunchViewSize
in your MainPage
's constructor like this:
public MainPage()
{
this.InitializeComponent();
ApplicationView.PreferredLaunchViewSize = new Size(480, 800);
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
}
As @kol also pointed out, if you want any size smaller than the default 500x320, you will need to manually reset it:
ApplicationView.GetForCurrentView().SetPreferredMinSize(new Size(200, 100));