Windows Phone 8 学习备忘(如何获取手机屏幕的物理尺寸)

WP8的机子升级到WP8.1的Preview后,发现6寸屏幕的机器,键盘弹起的高度和以前不一样了,App Bar的高度和里面的字体大小不一样了,为了适配这一个变化,必须得获取到手机屏幕的物理尺寸进行特殊处理,搜了一大圈,最后终于找到了这个间接的接口,微软你的API藏的够深的http://blogs.windows.com/windows_phone/b/wpdev/archive/2013/11/22/taking-advantage-of-large-screen-windows-phones.aspx

string GetExtendedScreenInfo()

{

  object temp;

  if (!DeviceExtendedProperties.TryGetValue("PhysicalScreenResolution", out temp))

    return "not available, sorry";

 

  var screenResolution = (Size)temp;

 

  // Can query for RawDpiY as well, but it will be the same value

  if (!DeviceExtendedProperties.TryGetValue("RawDpiX", out temp) || (double)temp == 0d)

    return "not available, sorry";

 

  var dpi = (double)temp;

  var screenDiagonal = Math.Sqrt(Math.Pow(screenResolution.Width / dpi, 2) +

  Math.Pow(screenResolution.Height / dpi, 2));

 

  var width = App.Current.Host.Content.ActualWidth;

 

  return String.Format("{0} x {1}; {2:0.0#} raw scale; {3:0.0}\"",

    screenResolution.Width, screenResolution.Height, screenResolution.Width / width,

    screenDiagonal);

}

 

posted on 2014-04-18 10:55  Le Ruin  阅读(1626)  评论(3编辑  收藏  举报