【问题标题】:Get Screen Resolution in Win10 UWP App在 Win10 UWP App 中获取屏幕分辨率
【发布时间】:2015-11-03 08:19:53
【问题描述】:

由于 UWP 应用在普通桌面系统上以窗口模式运行,因此获取屏幕分辨率的“旧”方式将不再适用。

Window.Current.Bounds 的旧分辨率类似于 shown in.

还有其他方法可以获取(主)显示器的分辨率吗?

【问题讨论】:

  • 您介意解释一下它不起作用是什么意思吗?
  • @Barptad Window.Current.Bounds 返回当前窗口的大小,而不是屏幕的大小。
  • 好奇地问。为什么需要知道分辨率?
  • 有几个用例。例如,如果您要创建桌面背景图像。我“只是”需要它来进行跟踪。

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


【解决方案1】:

为了进一步改进其他答案,以下代码还考虑了比例因子,例如用于我的 Windows 显示器的 200%(正确返回 3200x1800)和 Lumia 930 (1920x1080) 的 300%。

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

正如其他答案中所述,这只会在更改根框架的大小之前在桌面上返回正确的大小。

【讨论】:

  • rootFrame.SizeChanged var bounds = ApplicationView.GetForCurrentView().VisibleBounds;没有给我正确的解决方案。我在桌面上的比例是 100,所以这没有影响。
  • 稍微更新了我的答案。原因是其他答案的代码不负责缩放(我的 Windows 设备为 200%,Lumia 930 为 300%)并且在我的设备上是错误的。
  • 好的,这是肯定的。您可以再次编辑它并提及它仅在更改根框架的大小之前有效。比你的答案将是完整的解决方案;)
  • 啊,我明白了。是的,最好在Window.Current.Activate(); 之后或在根帧大小更改之前运行的另一部分代码中添加它(这就是为什么它在我刚刚尝试过的几个 ViewModel 构造函数中工作......)。
  • 只是为了改进答案,您可以将 bounds.Height 更改为 bounds.Bottom ,这会在 Windows Mobile 和 Desktop 窗口中提供精确的分辨率值。 var size = new Size(bounds.Width * scaleFactor, bounds.Bottom * scaleFactor);
【解决方案2】:

随时随地调用此方法(已在移动/桌面应用中测试):

public static Size GetCurrentDisplaySize() {
    var displayInformation = DisplayInformation.GetForCurrentView();
    TypeInfo t = typeof(DisplayInformation).GetTypeInfo();
    var props = t.DeclaredProperties.Where(x => x.Name.StartsWith("Screen") && x.Name.EndsWith("InRawPixels")).ToArray();
    var w = props.Where(x => x.Name.Contains("Width")).First().GetValue(displayInformation);
    var h = props.Where(x => x.Name.Contains("Height")).First().GetValue(displayInformation);
    var size = new Size(System.Convert.ToDouble(w), System.Convert.ToDouble(h));
    switch (displayInformation.CurrentOrientation) {
    case DisplayOrientations.Landscape:
    case DisplayOrientations.LandscapeFlipped:
        size = new Size(Math.Max(size.Width, size.Height), Math.Min(size.Width, size.Height));
        break;
    case DisplayOrientations.Portrait:
    case DisplayOrientations.PortraitFlipped:
        size = new Size(Math.Min(size.Width, size.Height), Math.Max(size.Width, size.Height));
        break;
    }
    return size;
}

【讨论】:

  • 仅供参考,不是任何时候任何时间:Windows.Graphics.Display:GetForCurrentView 必须在与 CoreWindow 关联的线程上调用。
  • 天才。这应该是公认的答案,因为当前不处理外部显示器中使用的应用程序。
【解决方案3】:

更简单的方法:

var displayInformation = DisplayInformation.GetForCurrentView();
var screenSize = new Size(displayInformation.ScreenWidthInRawPixels, 
                          displayInformation.ScreenHeightInRawPixels);

这不取决于当前视图大小。随时返回真实的屏幕分辨率。

【讨论】:

  • 谢谢,这是一个比这里所有其他人更好的答案。
【解决方案4】:

好的,Juan Pablo Garcia Coello 的回答引导我找到解决方案 - 谢谢!

你可以使用

var bounds = ApplicationView.GetForCurrentView().VisibleBounds;

但你必须在我的情况下立即显示窗口之前调用它

Window.Current.Activate();

是个好地方。此时,您将获得您的应用程序将出现的窗口的边界。

非常感谢您帮助我解决它:)

问候亚历克斯

【讨论】:

    【解决方案5】:

    我发现的唯一方法是在 Page 的构造函数中:

     public MainPage()
        {
            this.InitializeComponent();
    
            var test = ApplicationView.GetForCurrentView().VisibleBounds;
        }
    

    我还没有在 Windows 10 Mobile 中测试过,当新版本出现时我会测试它。

    【讨论】:

    • 这似乎不起作用。我的全高清屏幕宽度为 838,高度为 900。
    【解决方案6】:

    使用此方法获取屏幕尺寸:

    public static Size GetScreenResolutionInfo() 
    { 
        var applicationView = ApplicationView.GetForCurrentView(); 
        var displayInformation = DisplayInformation.GetForCurrentView(); 
        var bounds = applicationView.VisibleBounds; 
        var scale = displayInformation.RawPixelsPerViewPixel; 
        var size = new Size(bounds.Width * scale, bounds.Height * scale); 
        return size; 
    } 
    

    您应该在 Window.Current.Activate(); 之后从 App.xaml.cs 调用此方法;在 OnLaunched 方法中。

    这是示例代码,您可以download 完整项目。

    【讨论】:

      【解决方案7】:

      只需为主 Grid 或 Page 设置名称,然后为您想要的元素调用其宽度或高度:

      Element.Height = PagePane.Height;
      Element.width = PagePane.Width;
      

      这是您可以使用的最简单的方法!

      【讨论】:

      • 问题是如何获得屏幕分辨率。 UWP 应用可以在 Windows 10 桌面上以 Windows 模式运行。所以 Windows 的大小并不是我所需要的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-15
      • 2010-12-03
      • 1970-01-01
      相关资源
      最近更新 更多