【问题标题】:How to get screen resolutions on Windows Phone devices如何在 Windows Phone 设备上获取屏幕分辨率
【发布时间】:2013-03-08 22:06:44
【问题描述】:

我遇到了问题,希望得到专家的帮助。我正在尝试获取屏幕分辨率,以便可以根据手机类型使用适当的布局/图像。

我的项目类型是 WP7。每当我在不同的 WP7 和 WP8 设备上运行代码时,我每次都得到相同的分辨率(800 X 480)。预期的行为是我根据设备类型获得不同的分辨率,例如WVGA = 800 x 480,WXGA = 1280 x 768,720p = 1280 x 720。

下面的所有 3 个代码 sn-ps 都给了我相同的 800 X 480 分辨率,这不是预期的行为。

Application.Current.RootVisual.RenderSize.Height + " x " + Application.Current.RootVisual.RenderSize.Width;

(App.Current.RootVisual as FrameworkElement).ActualHeight + " x " + (App.Current.RootVisual as FrameworkElement).ActualWidth;

App.Current.Host.Content.ActualHeight + " x " + App.Current.Host.Content.ActualWidth;

MSDN 文章讨论了如何在 WP8 中执行此操作,但请注意我希望代码也可以在 WP7 设备上运行。

【问题讨论】:

    标签: windows-phone-7 windows-phone


    【解决方案1】:

    我最终根据上面的 Anton Sizikov 建议编写了以下代码。它使用反射来读取 ScaleFactor 属性。如果 7.1 应用程序在 WP8 设备上运行,反射将返回 ScaleFactor 属性的值,并基于该设备分辨率可以确定。

    public enum Resolutions { WVGA, WXGA, HD720p };
    
    public static class ResolutionHelper
    {
        static int? ScaleFactor;
    
        static ResolutionHelper()
        {
            object scaleFactorValue = GetPropertyValue(App.Current.Host.Content, "ScaleFactor");
            if (scaleFactorValue != null)
            {
                ScaleFactor = Convert.ToInt32(scaleFactorValue);
            }
        }
    
        private static bool IsWvga
        {
            get
            {
                return ScaleFactor.HasValue && ScaleFactor.Value == 100;
            }
        }
    
        private static bool IsWxga
        {
            get
            {
                return ScaleFactor.HasValue && ScaleFactor.Value == 160;
            }
        }
    
        private static bool Is720p
        {
            get
            {
                return ScaleFactor.HasValue && ScaleFactor.Value == 150;
            }
        }
    
        public static Resolutions CurrentResolution
        {
            get
            {
                if (IsWxga) return Resolutions.WXGA;
                else if (Is720p) return Resolutions.HD720p;
                return Resolutions.WVGA;
            }
        }
    
        private static object GetPropertyValue(object instance, string name)
        {
            try
            {
                return instance.GetType().GetProperty(name).GetValue(instance, null);
            }
            catch
            {
                // Exception will occur when app is running on WP7 devices as "ScaleFactor" property doesn't exist. Return null in that case. 
                return null;
            }
        }
    
    
    }
    

    【讨论】:

      【解决方案2】:

      如您链接到的 MSDN 文章中所述:MSDN

      Windows Phone 7 仅支持单一分辨率 800 x 480。由于您的项目以 WP7 为目标,这将是预期的行为。如果您正在开发 Windows Phone 8 项目,那么您应该会看到 App.Current.Host.Content.ScaleFactor 应该返回不同的结果。

      您可能需要创建一个 WP8 项目来自定义 WP8 中的各种分辨率。如果您仍想支持 WP7 设备,则需要创建一个单独的 WP7 项目。

      【讨论】:

      • 感谢您的意见。但是,我想避免创建多个项目。我希望使用单个 Windows Phone 7(WP7) 项目并创建一个在 WP7 和 WP8 设备上运行的应用程序。我的观察是针对 WP7 项目,它总是返回 800x480 的分辨率,无论它运行在什么硬件上。 WP7 SDK 中是否还有其他 API 可以让我读取屏幕分辨率?
      • 基本上不会,因为在 WP8 上运行 WP7 应用时,该应用总是被限制为 800x480。
      【解决方案3】:

      您可以尝试使用反射加载App.Current.Host.Content.ScaleFactor

      我现在没有我的wp8环境,但是你可以看到类似的解决方案here。他们用它在 wp7.8 上创建王尔德瓷砖

      【讨论】:

      • 感谢 Anton 提出宝贵意见。这种方法对我有用。
      猜你喜欢
      • 1970-01-01
      • 2014-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多