C# 如何获得屏幕的分辨率?对于 WinRT 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10828179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the resolution of screen? For a WinRT app?
提问by a_rahmanshah
I want to know the screen resolution so that I can set the height of an element according to the resolution in a Windows 8 app.
我想知道屏幕分辨率,以便我可以根据 Windows 8 应用程序中的分辨率设置元素的高度。
采纳答案by Krishna
How about this?
这个怎么样?
var bounds = Window.Current.Bounds;
double height = bounds.Height;
double width = bounds.Width;
回答by Shawn Kendrot
Are you using XAML? If so it does not matter. Use the Grid control. It will fill up all available space. Read Jerry's blogas to why you might want to use xaml for WinRT development.
你在使用 XAML 吗?如果是这样也没关系。使用网格控件。它将填满所有可用空间。阅读Jerry 的博客,了解您可能想要使用 xaml 进行 WinRT 开发的原因。
回答by steveb
apparently i don't have enough rep to reply to posts yet, but in regards to @Krishna's answer, it may be worth noting that his solution requires:
显然我还没有足够的代表来回复帖子,但关于@Krishna 的回答,可能值得注意的是,他的解决方案需要:
using Windows.UI.Xaml;
probably not an issue for most of you, but in my case (trying to grab resolution of executing app from an imported library), it wasn't there by default.
对你们大多数人来说可能不是问题,但在我的情况下(试图从导入的库中获取执行应用程序的分辨率),默认情况下它不存在。
hope this helps someone else...
希望这对其他人有帮助...
回答by Anton
Probably the best option for DirectX-enabled apps, however, applicable to all other kinds of metro apps is:
可能是启用 DirectX 的应用程序的最佳选择,然而,适用于所有其他类型的 Metro 应用程序是:
P.S. C'mon, getting window size to determine screen resolution? What about snapped/filled modes? This world is so much broken :-/
PS来吧,获取窗口大小来确定屏幕分辨率?捕捉/填充模式怎么样?这个世界太破碎了:-/
回答by Thiru
Getting the bounds of current window is easy. But say if you want to set a large font size for bigger screen (resolution is same as 10" device, but screen is 27"), this wont help. Refer Scaling to different screensI used the following method to detect screen size & change text block font style appropriately.
获取当前窗口的边界很容易。但是,如果您想为更大的屏幕设置大字体(分辨率与 10" 设备相同,但屏幕为 27"),这将无济于事。参考缩放到不同的屏幕我使用以下方法来检测屏幕大小并适当更改文本块字体样式。
void detectScreenType()
{
double dpi = DisplayProperties.LogicalDpi;
var bounds = Window.Current.Bounds;
double h;
switch (ApplicationView.Value)
{
case ApplicationViewState.Filled:
h = bounds.Height;
break;
case ApplicationViewState.FullScreenLandscape:
h = bounds.Height;
break;
case ApplicationViewState.Snapped:
h = bounds.Height;
break;
case ApplicationViewState.FullScreenPortrait:
h = bounds.Width;
break;
default:
return;
}
double inches = h / dpi ;
string screenType = "Slate";
if (inches < 10)
{
screenType = "Slate";
} else if (inches < 14) {
screenType = "WorkHorsePC";
}
else
{
screenType = "FamilyHub";
}
ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
localSettings.Values["screenType"] = screenType;
}

