Windows 10 UWP app - Setting window size on deskto

2019-01-13 02:44发布

问题:

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?

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

Update

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


回答2:

Hi I have solution to your problem, the thing is that 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

https://social.msdn.microsoft.com/Forums/en-US/3110e17d-b7c8-4040-8e25-a27df7595f13/windows-10-universal-directx-application?forum=wpdevelop

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 "OnActivated()" event handler as it will set your defined size when app starts and when it become Active after any interruptions.

In "desiredSize" Calculation 800 is width and 600 is height this calculation is needed because 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"



回答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.