在Win10 UWP应用中获取屏幕分辨率

33
作为一种在普通桌面系统上以窗口模式运行的UWP应用程序,获取屏幕分辨率的"旧"方法将不再可行。使用 Window.Current.Bounds 获取的旧分辨率可以参考此处。是否有其他方法来获取(主)显示器的分辨率呢?

1
你介意解释一下它为什么不起作用吗? - Barptad
1
@Barptad Window.Current.Bounds 返回的是当前窗口的大小,而不是屏幕的大小。 - Igor Ralic
2
好奇问一句,你为什么需要知道分辨率? - Justin XL
有几种用例。例如,如果您想创建桌面背景图像。我“只是”需要它来进行跟踪。 - Alex Witkowski
7个回答

61

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

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,因此这并不会影响。 - Alex Witkowski
稍微更新了一下我的答案。原因是其他答案的代码没有考虑到缩放问题(在我的Windows设备上为200%,在Lumia 930上为300%),并且在我的设备上出现了错误。 - sibbl
1
好的,那肯定没错。您可以再编辑一次并提到它只适用于根框架大小改变之前。那么您的答案将是完整的解决方案 ;) - Alex Witkowski
啊,我明白了。是的,最好在Window.Current.Activate();之后添加它,或者在代码的另一个部分运行,该部分在根框架大小更改之前运行(这就是为什么在几个ViewModel构造函数中尝试它时它能够工作...)。 - sibbl
3
为了改善答案,你可以将bounds.Height更改为bounds.Bottom,这会在Windows Mobile和桌面窗口中提供精确的分辨率值。var size = new Size(bounds.Width * scaleFactor, bounds.Bottom * scaleFactor); - Mahender
显示剩余2条评论

12

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

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 关联的线程上调用。 - T.S
1
天才。这应该是被接受的答案,因为当前的答案无法处理在外部监视器中使用的应用程序。 - Justin XL

11

更简单的方法:

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

这不取决于当前视图大小。它在任何时候都返回真实的屏幕分辨率。


谢谢,这个回答比这里所有其他的回答都好多了。 - Stef

5

好的,先感谢Juan Pablo Garcia Coello提供的答案,它让我找到了解决方案-非常感谢!

您可以使用

var bounds = ApplicationView.GetForCurrentView().VisibleBounds;

在我的情况下,在窗口显示之前调用它,但你必须在此之前调用它。
Window.Current.Activate();

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

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

敬礼,Alex


2
我发现的唯一方法是在一个页面的构造函数内部:
 public MainPage()
    {
        this.InitializeComponent();

        var test = ApplicationView.GetForCurrentView().VisibleBounds;
    }

我还没有在Windows 10 Mobile上进行测试,等新版本发布时我会进行测试。


似乎不起作用。在我的全高清屏幕上,宽度为838,高度为900。 - Alex Witkowski

1
使用此方法获取屏幕大小:

Use this method to get the screen size:

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

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

以下是示例代码,您可以下载完整的项目。


-3

只需要为主网格或页面设置一个名称,并调用其宽度或高度,即可获得所需元素:

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

这是你可以使用的最简单的方式!


3
问题是如何获取屏幕分辨率。UWP应用可以在Windows 10桌面模式下运行,因此窗口大小并不完全符合我在这里所需的条件。 - Alex Witkowski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接