How to get screen resolutions on Windows Phone devices

前端 未结 3 1601
灰色年华
灰色年华 2021-01-22 03:53

I am running into issue and would appreciate expert’s help here. I am trying to get screen resolution so that I can use appropriate layouts/images based on phone types.

相关标签:
3条回答
  • 2021-01-22 04:15

    I ended up writing following code based Anton Sizikov recommendation above. It uses reflection to read ScaleFactor property. If 7.1 App is running on WP8 devices, reflection will return value for ScaleFactor property and based on that device resolution can be determined.

    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;
            }
        }
    
    
    }
    
    0 讨论(0)
  • 2021-01-22 04:33

    You can try load App.Current.Host.Content.ScaleFactor using reflection.

    I don't have my wp8 environment now, but you can see the similar solution here. They use it to create wilde tiles on wp7.8

    0 讨论(0)
  • 2021-01-22 04:35

    As stated in the MSDN article you linked to: MSDN

    Windows Phone 7 only supported a single resolution, 800 x 480. Since your project is targeting WP7, that would be the expected behavior. If you are developing a Windows Phone 8 project, then you should see that App.Current.Host.Content.ScaleFactor should return different results.

    You will probably need to create a WP8 project to customize for the various resolutions in WP8. If you still want to support WP7 devices, then you will need to create a separate WP7 project.

    0 讨论(0)
提交回复
热议问题