How to get screen resolution on Windows Phone devices

I ran into a problem and I will be grateful to the experts for their help here. I am trying to get screen resolution in order to use appropriate layouts / images based on phone types.

My project type is WP7. Whenever I run the code on different WP7 and WP8 devices, I get the same resolution every time (800 X 480). Expected Behavior - I get a different resolution based on the type of device, for example. WVGA = 800 x 480, WXGA = 1280 x 768, 720p = 1280 x 720.

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

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 keep in mind that I want the code to work on a WP7 device as well.

+2
source share
3 answers

You can try loading App.Current.Host.Content.ScaleFactorusing reflection.

Now I do not have my wp8 environment, but you can see a similar solution here . They use it to create wildlife tiles on wp7.8.

+1
source

In the end, I wrote the following recommendation according to the method of Anton Sizikov, cited above. It uses reflection to read the ScaleFactor property. If application 7.1 runs on WP8 devices, reflection will return a value for the ScaleFactor property and based on this device resolution.

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
source

MSDN, : MSDN

Windows Phone 7 800 x 480. WP7, . Windows Phone 8, , App.Current.Host.Content.ScaleFactor .

, , WP8 WP8. WP7, WP7.

+2

Source: https://habr.com/ru/post/1547914/


All Articles