×
Namespaces

Variants
Actions
(Difference between revisions)

How to detect the screen resolution on Windows Phone

From Nokia Developer Wiki
Jump to: navigation, search
dan.ardelean (Talk | contribs)
(Dan.ardelean - - Summary)
influencer (Talk | contribs)
(Influencer -)
(17 intermediate revisions by 5 users not shown)
Line 1: Line 1:
[[Category:Code Examples]][[Category:Code Snippet]][[Category:Windows Phone 8]][[Category:Windows Phone 7.5]][[Category:XAML]]
+
[[Category:Windows Phone]][[Category:UI]][[Category:How To]][[Category:Windows Phone 8]][[Category:Windows Phone 7.5]][[Category:XAML]][[Category:Code Examples]][[Category:System Information]]
{{Abstract|This article explains how to detect the display resolution from an Windows Phone OS 7.1 application}}
+
{{Abstract|This article explains how to detect the display resolution using reflection. The code can be used in Windows Phone 7 and later.}}
 
+
{{SeeAlso|[[Fullscreen XNA games on Windows Phone 8 devices]]}}
''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''
+
{{ArticleMetaData <!-- v1.3 -->
{{ArticleMetaData <!-- v1.2 -->
+
|sourcecode= [[Media:DisplayResolution.zip]]
|sourcecode= [[File:DisplayResolution.zip]]
+
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
+
|devices= Lumia 920, Lumia 800
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Qt SDK 1.1.4]) -->
+
|sdk= Windows Phone SDK 8.0
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
+
|platform= Windows Phone 7 and later
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->  
+
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
+
|capabilities= <!-- Required capabilities for code (e.g. ID_CAP_LOCATION, ID_CAP_NETWORKING) -->
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
+
 
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
 
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|translated-by= <!-- [[User:XXXX]] -->
 
|translated-by= <!-- [[User:XXXX]] -->
|translated-from-title= <!-- Title only -->  
+
|translated-from-title= <!-- Title only -->
 
|translated-from-id= <!-- Id of translated revision -->
 
|translated-from-id= <!-- Id of translated revision -->
|review-by=<!-- After re-review: [[User:username]] -->
+
|review-by= <!-- After re-review: [[User:username]] -->
 
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 
|update-by= <!-- After significant update: [[User:username]]-->
 
|update-by= <!-- After significant update: [[User:username]]-->
 
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 
|update-timestamp= <!-- After significant update: YYYYMMDD -->
|creationdate= <!-- Format YYYYMMDD -->
+
|creationdate= 20130430
|author= <!-- Display as link [[User:username]] -->
+
|author= [[User:dan.ardelean]]
 +
<!-- The following are not in current metadata -->
 +
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 
}}
 
}}
  
== Introduction ==
+
== Overview ==
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.
+
Windows Phone 7 apps can be run on Windows Phone 8 devices, and will usually display "acceptably" on the new display resolutions supported by Windows Phone 8. However if you want to take advantage of the new screen display resolutions, for example using higher quality images for higher resolution devices, you will need to know the {{Icode|ScaleFactor}} of the device.  
  
== Summary ==
+
The property is only available in the Windows Phone 8.0 SDK, but if you need to access it on a Windows Phone 7 app you can do so using [http://en.wikipedia.org/wiki/Reflection_(computer_programming) reflection].
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:
+
== Solution ==
  public static Size DisplayResolution   
+
Retrieve the {{Icode|Get()}} method of the {{Icode|ScaleFactor}} property using reflection and invoke it only if the application is running on a Windows Phone 8 device.
 +
 
 +
<code csharp>
 +
public static Size DisplayResolution   
 
     {   
 
     {   
 
       get   
 
       get   
Line 39: Line 41:
 
         if (Environment.OSVersion.Version.Major<8)   
 
         if (Environment.OSVersion.Version.Major<8)   
 
           return new Size(480,800);   
 
           return new Size(480,800);   
         int scaleFactor=(int) GetProperty(Application.Current.Host.Content, "ScaleFactor");   
+
         int scaleFactor=(int) GetProperty(System.Windows.Application.Current.Host.Content, "ScaleFactor");   
 
         switch (scaleFactor)   
 
         switch (scaleFactor)   
 
         {   
 
         {   
Line 57: Line 59:
 
       return getMethod.Invoke(instance, null);   
 
       return getMethod.Invoke(instance, null);   
 
     }   
 
     }   
 +
</code>
 +
 +
== Test Code ==
 +
 +
The test code can be downloaded from here: [[File:DisplayResolution.zip]]
 +
 +
The Emulator output of the test code is shown below.
 
   
 
   
<gallery>
+
[[File:DisplayResolution.png|thumb|none|300px|Emulator]]
File:DisplayResolution.png|Emulator
+
 
</gallery>
+
== References ==
 +
 
 +
* [http://www.developer.nokia.com/Resources/Library/Lumia/#!how-to-adapt-to-lumia-phone-hardware-features/how-to-check-characteristics-of-hardware-features.html How to check characteristics of hardware features] (Lumia Developer's Library)
 +
* [http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj206974(v=vs.105).aspx Multi-resolution apps for Windows Phone 8] (Dev Center)
 +
* [http://sviluppomobile.blogspot.com.au/2013/04/detect-screen-resolution-for-windows.html Detect the screen resolution for Windows Phone OS 7.1 applications] (Original blog)
 +
 
 +
[[ar:كيفية تحديد دقه الشاشة في الويندوز فون]]

Revision as of 23:57, 16 June 2013

This article explains how to detect the display resolution using reflection. The code can be used in Windows Phone 7 and later.

WP Metro Icon UI.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Code ExampleTested with
SDK: Windows Phone SDK 8.0
Devices(s): Lumia 920, Lumia 800
Compatibility
Platform(s): Windows Phone 7 and later
Windows Phone 8
Windows Phone 7.5
Article
Created: dan.ardelean (30 Apr 2013)
Last edited: influencer (16 Jun 2013)

Contents

Overview

Windows Phone 7 apps can be run on Windows Phone 8 devices, and will usually display "acceptably" on the new display resolutions supported by Windows Phone 8. However if you want to take advantage of the new screen display resolutions, for example using higher quality images for higher resolution devices, you will need to know the ScaleFactor of the device.

The property is only available in the Windows Phone 8.0 SDK, but if you need to access it on a Windows Phone 7 app you can do so using reflection.

Solution

Retrieve the Get() method of the ScaleFactor property using reflection and invoke it only if the application is running on a Windows Phone 8 device.

public static Size DisplayResolution  
{
get
{
if (Environment.OSVersion.Version.Major<8)
return new Size(480,800);
int scaleFactor=(int) GetProperty(System.Windows.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);
}

Test Code

The test code can be downloaded from here: File:DisplayResolution.zip

The Emulator output of the test code is shown below.

Emulator

References

294 page views in the last 30 days.
×