How to get screen resolutions on Windows Phone devices

前端 未结 3 1605
灰色年华
灰色年华 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;
            }
        }
    
    
    }
    

提交回复
热议问题