Wednesday 24 April 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

0 comments:

Post a Comment