wolfgang ziegler


„make stuff and blog about it“

Windows Phone: Detect if your app is running inside the emulator

February 25, 2013

Sometimes, for debugging reasons, it is helpful for your Windows Phone app to know if it is running inside the emulator or on a real device.

The property Microsoft.Devices.Environment.DeviceType gives us exactly this information. Possible values for this enumeration are DeviceType.Emulator and DeviceType.Type.

I, for example, am working on an app right now, that grabs frames from my Windows Phone’s camera and processes them. Unfortunately, camera mode on the emulator just gives us a gray screen with a colored rectangle moving around.

image

For testing my app I need a bit more than that though. That’s were detecting the emulator comes in handy and I can fake previously stored images to my screen grab code.

For simplicity, I made this device information a simple Boolean property in the base class for all my ViewModels, so I can use them throughout the whole code.

It’s tried and tested development practice to keep a couple of useful basic properties in your ViewModel base class to have them easily accessible and bindable in all your derived ViewModels.

Here’s the code of my base ViewModel class with its selection of basic properties I find quite helpful.

   1: public class ViewModelBase : DependencyObject, INotifyPropertyChanged
   2: {
   3:   public event PropertyChangedEventHandler PropertyChanged;
   4:  
   5:   private LicenseInformation _licenseInformation;
   6:   private bool _isBusy;
   7:  
   8:   public ViewModelBase()
   9:   {
  10:     _licenseInformation = new LicenseInformation();
  11:   }
  12:  
  13:   public bool IsDebug
  14:   {
  15:     get
  16:     {
  17: #if DEBUG
  18:       return true;
  19: #else
  20:       return false;
  21: #endif
  22:     }
  23:   }
  24:  
  25:   public bool IsEmulator
  26:   {
  27:     get
  28:     {
  29:       return Environment.DeviceType == DeviceType.Emulator;
  30:     }
  31:   }
  32:  
  33:   public bool IsTrial
  34:   {
  35:     get
  36:     {
  37:       return IsDebug ? true : _licenseInformation.IsTrial();
  38:     }
  39:   }
  40:  
  41:   public bool IsDesignMode
  42:   {
  43:     get
  44:     {
  45:       return DesignerProperties.IsInDesignTool;
  46:     }
  47:   }
  48:  
  49:   public bool IsBusy
  50:   {
  51:     get
  52:     {
  53:       return _isBusy;
  54:     }
  55:     set
  56:     {
  57:       _isBusy = value;
  58:       OnPropertyChanged("IsBusy");
  59:     }
  60:   }
  61:  
  62:   protected void OnPropertyChanged(string propertyName)
  63:   {
  64:     var handler = PropertyChanged;
  65:     if (handler != null)
  66:     {
  67:       handler(this, new PropertyChangedEventArgs(propertyName));
  68:     }
  69:   }
  70: }