作为一种在普通桌面系统上以窗口模式运行的UWP应用程序,获取屏幕分辨率的"旧"方法将不再可行。使用
Window.Current.Bounds
获取的旧分辨率可以参考此处。是否有其他方法来获取(主)显示器的分辨率呢?Window.Current.Bounds
获取的旧分辨率可以参考此处。是否有其他方法来获取(主)显示器的分辨率呢?为了进一步改善其他答案,以下代码还考虑了缩放因素,例如我的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);
正如其他答案所述,在根框架大小改变之前,这只会在台式机上返回正确的大小。
Window.Current.Activate();
之后添加它,或者在代码的另一个部分运行,该部分在根框架大小更改之前运行(这就是为什么在几个ViewModel构造函数中尝试它时它能够工作...)。 - sibblvar size = new Size(bounds.Width * scaleFactor, bounds.Bottom * scaleFactor);
- Mahender随时随地调用此方法(已在移动/桌面应用程序中进行测试):
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;
}
更简单的方法:
var displayInformation = DisplayInformation.GetForCurrentView();
var screenSize = new Size(displayInformation.ScreenWidthInRawPixels,
displayInformation.ScreenHeightInRawPixels);
这不取决于当前视图大小。它在任何时候都返回真实的屏幕分辨率。
好的,先感谢Juan Pablo Garcia Coello提供的答案,它让我找到了解决方案-非常感谢!
您可以使用
var bounds = ApplicationView.GetForCurrentView().VisibleBounds;
Window.Current.Activate();
这是一个不错的地方。此时,您将获得应用程序出现的窗口边界。
非常感谢您帮助我解决它 :)
敬礼,Alex
public MainPage()
{
this.InitializeComponent();
var test = ApplicationView.GetForCurrentView().VisibleBounds;
}
我还没有在Windows 10 Mobile上进行测试,等新版本发布时我会进行测试。
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调用此方法。
以下是示例代码,您可以下载完整的项目。
只需要为主网格或页面设置一个名称,并调用其宽度或高度,即可获得所需元素:
Element.Height = PagePane.Height;
Element.width = PagePane.Width;
这是你可以使用的最简单的方式!