37

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kol
  • 27,881
  • 12
  • 83
  • 120

4 Answers4

80

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));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Justin XL
  • 38,763
  • 7
  • 88
  • 133
  • 1
    Interesting, thanks. Is it possible to set this in XAML? – kol Aug 08 '15 at 08:55
  • Unfortunately I don't think so. – Justin XL Aug 08 '15 at 08:55
  • 1
    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. – Justin XL Aug 08 '15 at 09:26
  • 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 Aug 08 '15 at 09:28
  • 5
    This works: `ApplicationView.GetForCurrentView().SetPreferredMinSize(new Size(200, 100));` – kol Aug 08 '15 at 09:36
  • 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. – Justin XL Aug 08 '15 at 10:24
  • 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(Width), static_cast(Height) }; ApplicationView::PreferredLaunchWindowingMode = ApplicationViewWindowingMode::PreferredLaunchViewSize;` – thehelix Sep 07 '16 at 22:17
  • [Here](http://scottge.net/2015/08/29/controlling-the-window-size-of-uwp-windows-10-apps/) is a blog entry, which seems to take your answer as base and explain it a little bit more. – testing Oct 10 '16 at 13:13
  • 3
    If you're going to use the ApplicationView class, you need to include "using Windows.UI.ViewManagement" above your namespace declaration. – Robbie Smith Jan 13 '17 at 21:16
  • This works in the desktop mode of Windows 10 but not the Tablet mode in which case the app goes full screen. – François Mar 22 '17 at 07:23
  • @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. – Sam Hobbs Apr 26 '17 at 02:02
  • 7
    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) – binaryfunt Mar 14 '18 at 17:55
12

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rafal Rebisz
  • 189
  • 2
  • 7
4

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.

binaryfunt
  • 6,401
  • 5
  • 37
  • 59
  • 2
    The method will not work, as the "very first" page will be launched before enter into this constructor – chygo Apr 04 '19 at 06:44
0

In this other link here in StackOverflow, there is another way to do it: https://stackoverflow.com/a/68583688/5993426. This code is to insert in the App.xaml:

    protected override void OnWindowCreated(WindowCreatedEventArgs args)
    {
        SetWindowMinSize(new Size(args.Window.Bounds.Width, args.Window.Bounds.Height));
        args.Window.CoreWindow.SizeChanged += CoreWindow_SizeChanged;
        base.OnWindowCreated(args);
    }

    private void CoreWindow_SizeChanged(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.WindowSizeChangedEventArgs args)
    {
        if (SetWindowMinSize(args.Size))
        {
            sender.ReleasePointerCapture();
        }            
    }

    private bool SetWindowMinSize(Size size)
    {
        if (size.Width < minWidth || size.Height < minHeight)
        {
            if (size.Width < minWidth) size.Width = minWidth + 10;
            if (size.Height < minHeight) size.Height = minHeight + 10;
            return ApplicationView.GetForCurrentView().TryResizeView(size);
        }
        return false;
    }
Francesco
  • 47
  • 1
  • 4