Welcome to Professional ASP.NET - Chris Love's Official Blog Sign in | Join | Help

Chris Love's Official ASP.NET Blog

Chris Love's Helpful tips, tricks and pragmatic development knowledge for the ASP.NET world.
Add to Technorati Favorites


ASP Insider Follow Me On Twitter
Know If Your WP7 App is Running in the Emulator or Device at Run-Time

Windows Phone 7The Windows Phone 7 Emulator is a great way to test your applications before deploying them. However there are obvious limitations, such as working against the GPS system where the emulator just can’t quite cut it. In those situations it is common to create some sort of emulation routine to mimic the desired behavior, like changing GPS coordinates.

But once you have deployed your application to a real device you do not want your emulation routines to execute. Fortunately you can check for the DeviceType and avoid calling any emulator specific code you may have embedded.

Checking For the WP7 Device Type

The System.Devices.Environment.DeviceType property returns either DeviceType.Device or DeviceType.Emulator indicating what platform the application is running. From here you can make logical decisions to execute your code.

My advice is to check the Device Type when your application initially runs and store it in a global variable. I prefer doing this most of the time because it generally makes your application execute faster and eliminates redundant code. But I also found a reference to the call to DeviceType creating garbage, which means it will cause extra garbage collection to occur. This is even more overhead you want to avoid.

Here is a simple property I cobbled together to manage calling the DeviceType property.

int _deviceType;
public bool UseEmulation {

get {

if (0 == _deviceType) {

if (Microsoft.Devices.Environment.DeviceType
== DeviceType.Device) {
_deviceType = 1;
} else {
_deviceType = 2;
}

}

return !(_deviceType == 1);
}
}

So instead of calling the DeviceType property each time you needed it you would call the UseEmulation property which would rely on the _deviceType variable after the first call.

Posted: Wednesday, January 05, 2011 4:41 PM

by Chris Love
Filed under:

Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS