How to get screen resolutions on Windows Phone dev

2020-04-14 04:12发布

问题:

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.

My project type is WP7. Whenever I run code on different WP7 and WP8 devices, I get the same resolution everytime (800 X 480). Expected behavior is I get different resolution based on device type e.g. WVGA = 800 x 480, WXGA = 1280 x 768, 720p = 1280 x 720.

All the 3 code snippets below gave me same resolution of 800 X 480 which is not expected behavior.

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;

The MSDN article talks about how to do this in WP8 but please note that I want code to run on WP7 device as well.

回答1:

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



回答2:

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;
        }
    }


}


回答3:

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.