Wednesday, April 24, 2013

Detect the screen resolution for Windows Phone OS 7.1 applications

    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.

Solution:
We retrieve the Get method of the ScaleFactor property 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);  
     }  

I am also attaching a small sample Windows Phone OS 7.1 test project.


SOURCE CODE

P.S. I think Telerik could use this method to fix this RadToolTip visual anomaly (the 1 pixel width lines that I have filled with blue for contrast) :


NAMASTE

3 comments:

  1. Yes, it is a possibile solution for RadToolTip

    ReplyDelete
  2. Exactly what I was looking for!

    That solution is perfect for me since I still use SDK 7.1 (WP7.x) and I'll also benefit that it will work on WP8 devices with a single XAP to publish.

    Still like to continue support WP7.x devices ;-)

    ReplyDelete
  3. Much obliged to you such a great amount of for sharing.please observe this application. It could be fascinating for you,if you have an investment in singing. singalong applications
    mobile app development // mobile app developers // iPhone application

    development

    ReplyDelete