【问题标题】:How to ensure UWP app is always full screen on launch?如何确保 UWP 应用在启动时始终全屏显示?
【发布时间】:2017-10-31 14:35:53
【问题描述】:

有没有办法(C# 或 XAML)即使在我之前在桌面上调整大小并关闭 UWP 应用程序窗口后也可以最大化它?

我已尝试使用 ApplicationViewWindowingMode.FullScreen,但这会使应用程序全屏显示并覆盖 Windows 任务栏。

【问题讨论】:

    标签: c# xaml uwp windows-10 uwp-xaml


    【解决方案1】:

    您可以使用ApplicationViewWindowingMode 中的另一个值PreferredLaunchViewSize,然后设置ApplicationView.PreferredLaunchViewSize,但关键是要找出大小 将是什么。

    理论上,您可以使用一个非常大的数字,并且窗口会扩展到它可能的最大值。但是,以有效像素为单位计算屏幕尺寸可能更安全。

    因此,如果您只是在主Page 上调用以下方法之前 InitializeComponent();,它应该在启动时最大化窗口。

    private static void MaximizeWindowOnLoad()
    {
        // Get how big the window can be in epx.
        var bounds = ApplicationView.GetForCurrentView().VisibleBounds;
    
        ApplicationView.PreferredLaunchViewSize = new Size(bounds.Width, bounds.Height);
        ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
    }
    

    请注意,即使在您将其卸载后,该应用也会以某种方式记住这些设置。如果您想改回默认行为(应用以之前的窗口大小启动),只需调用一次 ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.Auto; 并删除所有代码。

    更新

    看起来在最新的 Windows 10 版本中,ApplicationView.GetForCurrentView().VisibleBounds 不再返回以有效像素为单位的 完整 窗口大小。所以我们现在需要一种新的方法来计算它。

    事实证明它非常简单,因为 DisplayInformation 类还为我们提供了屏幕分辨率和比例因子。

    以下是更新后的代码-

    public MainPage()
    {
        MaximizeWindowOnLoad();
    
        InitializeComponent();
    
        void MaximizeWindowOnLoad()
        {
            var view = DisplayInformation.GetForCurrentView();
    
            // Get the screen resolution (APIs available from 14393 onward).
            var resolution = new Size(view.ScreenWidthInRawPixels, view.ScreenHeightInRawPixels);
    
            // Calculate the screen size in effective pixels. 
            // Note the height of the Windows Taskbar is ignored here since the app will only be given the maxium available size.
            var scale = view.ResolutionScale == ResolutionScale.Invalid ? 1 : view.RawPixelsPerViewPixel;
            var bounds = new Size(resolution.Width / scale, resolution.Height / scale);
    
            ApplicationView.PreferredLaunchViewSize = new Size(bounds.Width, bounds.Height);
            ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
        }
    } 
    

    【讨论】:

    • 这对我不起作用的两个问题:(1)这仅在下次运行应用程序时应用。不是设置它的实际执行。我注意到甚至在调用 App 构造函数之前创建了应用程序窗口。 (2) VisibleBounds 只是返回窗口的大小。 (使用Microsoft.NETCore.UniversalWindowsPlatformv6.0.1)
    • @StevenJeuris 你说的(2)是什么意思?在不知道窗口大小的情况下如何最大化它?关于(1),如果它在您的场景中对您不起作用,请尝试找出一种方法并改进答案,否决它对任何人都没有帮助。
    • @JustinXL By (2) I mean that VisibleBounds returns "the visible region of the window (app view)",不是屏幕尺寸,即不是“窗口可以有多大”。我尝试让它工作(并且可能在将来),此时我会编辑或添加我自己的答案,但现在我只是留下评论提供更多信息(如果你相信,请随时将其包含在你的答案中这是正确的)。
    • @StevenJeuris 看起来 VisibleBounds 不再返回 epx 中的完整窗口大小。请参阅我的更新答案。
    • 我不得不调用 MaximizeWindowOnLoad();在 InitializeComponent(); 之后
    【解决方案2】:

    如果您想在启动时最大化您的应用,您可以使用以下方法:

    ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.Maximized;

    但请务必将其放入页面的 Loaded 事件中,否则将无法正常工作!

    【讨论】:

      【解决方案3】:

      我的分数太少,无法直接发表评论。以上都没有为我调整为最大化视图(或下面的单行 ApplicationViewWindowingMode.Maximized 方法),但我已经使用了一些答案来提出对我有用的东西。然而,它仍然非常笨重。 “DisplayInformation”中给出的屏幕尺寸太大,无法直接调整页面大小。尝试这样做没有用,我不得不将高度和宽度降低 60 以使其返回“真”,因此我有以下一些废话,它可能会帮助其他人找到更好的答案。它进入页面/窗口加载事件。无需在其他地方添加任何其他内容。

              private void Page_Loaded(object sender, RoutedEventArgs e)
          {
              var view = ApplicationView.GetForCurrentView();
              var displayInfo = DisplayInformation.GetForCurrentView();
              double x = ActualWidth;
              double y = ActualHeight;
              bool answer = true;
      
              // Get the screen resolution (APIs available from 14393 onward).
              var resolution = new Size(displayInfo.ScreenWidthInRawPixels-60, displayInfo.ScreenHeightInRawPixels-60);
      
              answer = view.TryResizeView(resolution); //This should return true if the resize is successful
              if (answer)
              {
                  x = displayInfo.ScreenWidthInRawPixels - 60;
                  y = displayInfo.ScreenHeightInRawPixels - 60;
              }
              answer = true;
              while (answer == true)
              {
                  x++;
                  answer = view.TryResizeView(new Size { Width = x, Height = y });
              }
              x = x - 1;
              answer = true;
              while (answer == true)
              {
                  y++;
                  answer = view.TryResizeView(new Size { Width = x, Height = y });
              }
      

      【讨论】:

        【解决方案4】:

        我有一个衬里,它可以像我预期的贾斯汀斯代码一样工作,但由于某种原因,当使用贾斯汀斯答案时,我的窗口不会被最大化......但后来我改变了一些让它最大化但我失去了一切我的流利设计,如亚克力和 RevealHighlite...

        所以我想出了一个让我所有流畅的设计原则都满意的衬里:

        ApplicationView.GetForCurrentView().TryEnterFullScreenMode();
        

        注意事项:

        我确实尝试过 Justins 的回答,并且我正在使用他在 initializeComponent() 之后直接调用的 MaximizeWindowOnLoad() 方法;

        完整概述:

        public class()
                {     
                    this.InitializeComponent();
                    MaximizeWindowOnLoad();   
                }
        
        private static void MaximizeWindowOnLoad()
                { 
                   ApplicationView.GetForCurrentView().TryEnterFullScreenMode();
                }
        

        【讨论】:

        • 杰西卡,这个方法应该解决你的窗口覆盖任务栏,额外的好处,你的亚克力风格将保持xD
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-06
        • 2012-11-28
        • 1970-01-01
        相关资源
        最近更新 更多