Setting window size on desktop for a Windows 10 UWP app

51,694

Solution 1

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

Solution 2

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".

Solution 3

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.

Share:
51,694
kol
Author by

kol

Updated on July 20, 2022

Comments

  • kol
    kol almost 2 years

    I've just started learning UWP app development on Windows 10 Pro using Visual Studio 2015 Community Edition. I tried to modify the C# version of the official "Hello, World!" sample by setting the Width and Height attributes of the Page tag in MainPage.xaml.

    Interestingly, when I start the app, its size will be different. Moreover, if I resize its window and then restart it, the app seems to remember its previous window size.

    Is it possible to force a UWP app to have a predefined window size, at least on desktop PCs?

  • kol
    kol over 8 years
    Interesting, thanks. Is it possible to set this in XAML?
  • Justin XL
    Justin XL over 8 years
    Unfortunately I don't think so.
  • Justin XL
    Justin XL over 8 years
    When you think about, you don't set the size on the main page, you set on the main page' parent's parent, which is the Frame's parent. I don't think there's direct xaml access for that.
  • kol
    kol over 8 years
    I see. Anyway: 1) To be precise, this Size will be the size of the MainPage, not the size of the window. For example, if I set it to 800x600, the window size will be 802x633, because the window will have a 1px wide border and a 31px high title bar (I use the default Win10 theme). 2) With this technique only, the MainPage cannot be made smaller than 500x320, even if it has no controls on it. Are there any MinWidth/MinHeight settings somewhere?
  • kol
    kol over 8 years
    This works: ApplicationView.GetForCurrentView().SetPreferredMinSize(new Size(200, 100));
  • Justin XL
    Justin XL over 8 years
    OK, my previous comment was a bit confusing. What I meant was, purely setting the size on the page will not work (the window will still be whatever size it's defaulted to be). The size must come from its ancestor, then flows down. Since there's no padding or margin on any of the elements in between, the size of the window will be the exact size of the main page. And you are absolutely right, if it's smaller than 500*320, you will need to set the min size manually.
  • jogo
    jogo over 8 years
    Link-only answers are not the way of SO. Links may be outdated someday. Put essential informations in your answer!
  • thehelix
    thehelix over 7 years
    For those looking for the C++ equivalent like I was, this will set the preferred launch view size to the Width and Height set in the Xaml for the page. ApplicationView::PreferredLaunchViewSize = Size{ static_cast<float>(Width), static_cast<float>(Height) }; ApplicationView::PreferredLaunchWindowingMode = ApplicationViewWindowingMode::PreferredLaunchViewSize;
  • testing
    testing over 7 years
    Here is a blog entry, which seems to take your answer as base and explain it a little bit more.
  • Robbie Smith
    Robbie Smith over 7 years
    If you're going to use the ApplicationView class, you need to include "using Windows.UI.ViewManagement" above your namespace declaration.
  • François
    François about 7 years
    This works in the desktop mode of Windows 10 but not the Tablet mode in which case the app goes full screen.
  • Sam Hobbs
    Sam Hobbs about 7 years
    @JustinXL, in the context of "think about" it, I think it helps to point out that a UWP Page is not the same as a WPF Window.
  • binaryfunt
    binaryfunt about 6 years
    Why not put this in App.xaml.cs, inside OnLaunched(LaunchActivatedEventArgs e) { }, since it's more to do with the App itself rather than the MainPage? (correct me if I'm wrong)
  • chygo
    chygo about 5 years
    The method will not work, as the "very first" page will be launched before enter into this constructor