【问题标题】:UWP : Bounds doesn't return actual Screen ResolutionUWP:边界不返回实际的屏幕分辨率
【发布时间】:2016-02-14 16:17:45
【问题描述】:

我正在开发一个 UWP 应用程序,我必须获得用户设置的实际屏幕分辨率(例如 1600 x 900)。我在 stackoverflow 上遇到了许多类似的问题,在我的代码中尝试了它们,但没有任何帮助。

要获取当前屏幕分辨率,我有以下代码:

     var bounds = ApplicationView.GetForCurrentView().VisibleBounds;
     var scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
     var size = new Size(bounds.Width * scaleFactor, bounds.Height * scaleFactor);
     Height = size.Height;
     Width = size.Width;

我的桌面分辨率设置为 1600 x 900。使用此代码,我得到高度为 828,宽度为 1600(即 1600 x 828)。但我的要求是获得用户设置的实际分辨率,即 1600 x 900。请指导我如何实现。

谢谢

【问题讨论】:

  • 你考虑过屏幕底部的任务栏吗?如果您将其固定,您将无法收回该房地产。 72 像素听起来很熟悉。
  • @RobertHarvey 我没有考虑任务栏。在 WPF(使用 PrimaryScreenHeight 等)中获得实际的屏幕分辨率非常容易,但在 UWP 中却没有。此外,这个 72 的值会在不同的分辨率下发生变化(因为我的应用程序是用于平板电脑的)。感谢您指出任务栏,但用户可能会或可能不会固定任务栏。我该如何解决(获取任务栏高度)?
  • 你试过this吗?
  • @RobertHarvey 是的!正如我已经提到的,我在 Stack OverFlow 上尝试过类似的问题。这是我尝试过的方法之一。这会产生相同的结果 (1600 x 828)。
  • @RobertHarvey 又一更新!!我在桌面上切换到平板电脑模式后尝试运行相同的代码,现在它给出的分辨率是 1600 x 860。我很困惑如何处理桌面和平板电脑模式?

标签: windows screen-resolution uwp


【解决方案1】:

为了得到实际的分辨率,你需要调用ApplicationView.GetForCurrentView().VisibleBoundsbefore窗口真正显示。更好的地方是在 Window.Current.Activate() 方法之后的 App.xaml.cs 中。 这个post 非常清楚地解释了启动窗口的大小。

下面是我的测试代码:

In App.xaml.cs:
           //here set preferred size = 800*800 for test
            ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
            ApplicationView.PreferredLaunchViewSize = new Size(800, 800);

            Window.Current.Activate();
           //here to get the real full screen size
            var bounds = ApplicationView.GetForCurrentView().VisibleBounds;
            var full = ApplicationView.GetForCurrentView().IsFullScreen;
            var scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
            var size = new Size(bounds.Width * scaleFactor, bounds.Height * scaleFactor);

in my Mainpage.xaml.cs:
        //here the size should be 800 * 800
        private void Button_Click(object sender, RoutedEventArgs e)
        {
           var bounds = ApplicationView.GetForCurrentView().VisibleBounds;
           var full = ApplicationView.GetForCurrentView().IsFullScreen;
           var scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
           var size = new Size(bounds.Width * scaleFactor, bounds.Height * scaleFactor);
       }

另外,关于你的问题,如何处理桌面和平板模式,你能澄清一下你想处理哪种情况吗?如果想根据不同的屏幕分辨率适配UI布局,可以参考MSDN在线帮助adaptive UI

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-31
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多