×
Namespaces

Variants
Actions
Revision as of 09:29, 30 April 2013 by dan.ardelean (Talk | contribs)

How to detect the screen resolution on Windows Phone

From Nokia Developer Wiki
Jump to: navigation, search

This article explains how to detect the display resolution from an Windows Phone OS 7.1 application

Enter article metadata as described below. Note that this template can be placed anywhere in the article. Do not remove parameters that you do not use

WP Metro Icon UI.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Code ExampleCompatibilityArticle
Created: (01 May 2013)
Last edited: dan.ardelean (30 Apr 2013)

Introduction

As you probably know Windows Phone 7.1 applications run perfectly on Windows Phone 8 devices. This means that your application/s could run on multiple display resolutions (which are the resolutions supported by Windows Phone 8 devices). If you want to take advantage of the new resolutions (for example downloading higher quality images for higher resolutions devices, fix some minor UI anomalies on higher resolutions) you will need to know the ScaleFactor of the device. The property is only available in the Windows Phone 8.0 SDK, but the good news is that you can access it using reflection.

Summary

We retrieve the Get method of the ScaleFactor property using refelection and invoke it only if the application is running on an Windows Phone 8 device.

Here is the source code:

 public static Size DisplayResolution  
    {  
      get  
      {  
        if (Environment.OSVersion.Version.Major<8)  
          return new Size(480,800);  
        int scaleFactor=(int) GetProperty(Application.Current.Host.Content, "ScaleFactor");  
        switch (scaleFactor)  
        {  
          case 100:  
            return new Size(480, 800);  
          case 150:  
            return new Size(720, 1280);  
          case 160:  
            return new Size(768, 1280);  
        }  
        return new Size(480, 800);  
      }  
    }  
    private static object GetProperty(object instance, string name)  
    {  
      var getMethod= instance.GetType().GetProperty(name).GetGetMethod();  
      return getMethod.Invoke(instance, null);  
    }  

221 page views in the last 30 days.
×