Ale*_*ski 26 c# xaml windows-runtime windows-10 uwp
由于UWP App在普通桌面系统上以窗口模式运行,因此获取屏幕分辨率的"旧"方式将不再适用.
旧的分辨率与Window.Current.Bounds当时想显示英寸
是否有另一种方法来获得(主要)显示器的分辨率?
sib*_*bbl 52
为了进一步改善其他答案,下面的代码也会考虑缩放因子,例如我的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);
Run Code Online (Sandbox Code Playgroud)
如其他答案中所述,这只会在更改根帧大小之前在桌面上返回正确的大小.
小智 10
随时随地调用此方法(在移动/桌面应用程序中测试):
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;
}
Run Code Online (Sandbox Code Playgroud)
小智 6
更简单的方法:
var displayInformation = DisplayInformation.GetForCurrentView();
var screenSize = new Size(displayInformation.ScreenWidthInRawPixels,
displayInformation.ScreenHeightInRawPixels);
Run Code Online (Sandbox Code Playgroud)
这不取决于当前视图的大小。在任何时候,它都会返回真实的屏幕分辨率。
好的,Juan Pablo Garcia Coello的回答引导我找到解决方案 - 谢谢!
您可以使用
var bounds = ApplicationView.GetForCurrentView().VisibleBounds;
Run Code Online (Sandbox Code Playgroud)
但是你必须在我的情况下显示窗口之前调用它
Window.Current.Activate();
Run Code Online (Sandbox Code Playgroud)
是个好地方。此时,您将获得应用将出现的窗口的边界。
非常感谢帮助我解决它:)
问候亚历克斯