简体   繁体   中英

How to ensure C# UWP app is full screen on the FIRST launch after the installation?

I am writing an UWP C# application. I am trying to fix my window size during the app launch. To achieve this, I am resizing the window in App.xaml.cs file. Below, the method for the resizing the window.

private void MaximizeWindowOnLoad()
{
    var displayInformation = DisplayInformation.GetForCurrentView();
    var widthOfScreen = displayInformation.ScreenWidthInRawPixels;
    var heightOfScreen = displayInformation.ScreenHeightInRawPixels;
    var scaleFactor = displayInformation.RawPixelsPerViewPixel;
    var bounds = new Size(widthOfScreen / scaleFactor, heightOfScreen / scaleFactor);
    var currentApplicationView = ApplicationView.GetForCurrentView();
    currentApplicationView.SetPreferredMinSize(bounds);
    ApplicationView.PreferredLaunchViewSize = new Size(bounds.Width, bounds.Height);
    ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
    currentApplicationView.TryResizeView(bounds);
}

I call MaximizeWindowOnLoad from OnLaunched method in App.xaml.cs. Below the OnLaunched method.

protected override void OnLaunched(LaunchActivatedEventArgs e)
{
    
    Frame rootFrame = Window.Current.Content as Frame;

    // Do not repeat app initialization when the Window already has content,
    // just ensure that the window is active
    if (rootFrame == null)
    {
        // Create a Frame to act as the navigation context and navigate to the first page
        rootFrame = new Frame();

        rootFrame.NavigationFailed += OnNavigationFailed;

        if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
        {
            //TODO: Load state from previously suspended application
        }

        // Place the frame in the current Window
        Window.Current.Content = rootFrame;
    }

    if (e.PrelaunchActivated == false)
    {
        if (rootFrame.Content == null)
        {
            // When the navigation stack isn't restored navigate to the first page,
            // configuring the new page by passing required information as a navigation
            // parameter
            rootFrame.Navigate(typeof(MainPage), e.Arguments);
        }
        // Ensure the current window is active
        MaximizeWindowOnLoad();

        Window.Current.Activate();
    }
}

The code works fine except one case. After installing the application, on the first launch, the code does not work. I checked, and during debugging the method is being called. I am using the methods from the following thread in stackoverflow. LINK .

I tried to debug to understand the issue. However, the behavior of the method is different for app opening cases (eg, opening at first and following times). I expected the code to work correct but I don't understand whether it is the problem of UWP or the method is incorrect.

There are two kind of ways that you are using in your code to change the windows size. One is ApplicationViewWindowingMode.PreferredLaunchViewSize and another is TryResizeView .

The behavior you got should be the effect of ApplicationViewWindowingMode.PreferredLaunchViewSize . I think the TryResizeView returns false so it actually has no effect.

PreferredLaunchViewSize

This API is used to set a value to indicate the windowing mode the app launches with. But as it is already mentioned in the Remark part of the document- For the very first launch of an app the PreferredLaunchWindowingMode will always be Auto and the ApplicationView.PreferredLaunchViewSize will be determined by system policies. The API applies to the next launch of the app.

So it's pretty clear about your behavior. The PreferredLaunchViewSize only works when the second time you launch your app. The size of the app is decided by the system when you launch the app for the first time.

TryResizeView

This API is used to change the size of the view to the specified size. It can work in the first time when you launch your app. But in your scenario, it returns false so it does nothing.

The reason why it fails is also mentioned in the Remark part of the document- The resize request has no effect and the method returns false in these cases: The requested size is larger than the available work area.

The available work area is usually smaller than the full screen size due to some reason like the TaskBar. For example, my monitor is 1920*1080 and the scale value is 100%. If I want to call this method successfully, the Max size I could use is 1904*991 (Tested).

Summary

If you want to change the size of your window at the first launch, you need to call TryResizeView method and give it a size which is smaller than the full screen size. And set the PreferredLaunchViewSize as full screen size.

When the second time it launches, stop calling the TryResizeView method and let the PreferredLaunchViewSize method changes the windows size to full screen automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM