kol
kol

Reputation: 28678

Setting window size on desktop for a Windows 10 UWP app

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?

Upvotes: 41

Views: 60159

Answers (4)

Francesco
Francesco

Reputation: 77

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

Upvotes: 0

Rafal Rebisz
Rafal Rebisz

Reputation: 199

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

Upvotes: 12

Justin XL
Justin XL

Reputation: 39006

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

Upvotes: 82

binaryfunt
binaryfunt

Reputation: 7127

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.

Upvotes: 4

Related Questions