繁体   English   中英

如何确保 C# UWP 应用程序在安装后首次启动时全屏显示?

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

我正在写一个 UWP C# 应用程序。 我正在尝试在应用程序启动期间修复我的 window 大小。 为此,我正在调整 App.xaml.cs 文件中 window 的大小。 下面是调整 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);
}

我从App.xaml.cs 中的 OnLaunched方法调用MaximizeWindowOnLoad OnLaunched方法下方。

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

除一种情况外,代码工作正常。 安装应用程序后,在第一次启动时,代码不起作用。 我检查过,在调试过程中调用了该方法。 我在 stackoverflow 中使用以下线程中的方法。 链接

我试图调试以了解问题。 但是,该方法的行为对于应用程序打开情况是不同的(例如,第一次打开和随后打开)。 我希望代码可以正常工作,但我不明白这是 UWP 的问题还是方法不正确。

您在代码中使用两种方法来更改 windows 大小。 一个是ApplicationViewWindowingMode.PreferredLaunchViewSize ,另一个是TryResizeView

您得到的行为应该是ApplicationViewWindowingMode.PreferredLaunchViewSize的效果。 我认为TryResizeView返回 false 所以它实际上没有效果。

PreferredLaunchViewSize

此 API 用于设置一个值以指示应用程序启动时使用的窗口模式。 但正如文档的备注部分中已经提到的那样 -对于应用程序的首次启动,PreferredLaunchWindowingMode 将始终为 Auto,而 ApplicationView.PreferredLaunchViewSize 将由系统策略确定。 API 适用于应用程序的下一次启动。

所以很清楚你的行为。 PreferredLaunchViewSize仅在您第二次启动应用程序时有效。 当您第一次启动应用程序时,应用程序的大小由系统决定。

试试ResizeView

这个API是用来改变view的大小为指定大小的。 它可以在您启动应用程序时第一次运行。 但是在你的场景中,它返回false所以它什么都不做。

它失败的原因也在文档的备注部分中提到——调整大小请求无效,并且该方法在这些情况下返回 false:请求的大小大于可用工作区。

由于任务栏等原因,可用工作区通常小于全屏尺寸。 比如我的显示器是1920*1080 ,缩放值为100%。 如果我想成功调用此方法,我可以使用的最大尺寸是1904*991 (已测试)。

概括

如果你想在第一次启动时改变你的 window 的尺寸,你需要调用TryResizeView方法并给它一个小于全屏尺寸的尺寸。 并将PreferredLaunchViewSize设置为全屏大小。

第二次启动时,停止调用TryResizeView方法,让PreferredLaunchViewSize方法自动将windows尺寸变为全屏。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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