Getting Device Information in Windows Phone 7

December 19, 2011

no comments

Windows Phone 7 mandates a set of minimum requirements from devices it runs on, such as camera resolution, hardware graphics support, the existence of sensors such as microphone, GPS, touch, etc. However, sometimes it’s useful to know the exact specifications of a particular device the application is running on, so the application can optimize. As a simple example, running on the emulator or a real device can make a huge difference – maybe the application has a way of doing things differently when running on the emulator for testing purposes.

The device information is exposed primarily through the Microsoft.Phone.Info.DeviceStatus static class, where various properties provide some useful (and some not so useful) information. One oddity is that the device type (emulator or real device) is exposed through a different static property, Microsoft.Devices.Environment.DeviceType.

For demo purposes, I’ve created a simple class that wraps some of these properties in a type suitable for data binding (no {x:Static} on WP7 yet Sad smile):

  1. public class AppDeviceState {
  2.     public DeviceType DeviceType {
  3.         get {
  4.             return Microsoft.Devices.Environment.DeviceType;
  5.         }
  6.     }
  7.  
  8.     public string DeviceName {
  9.         get { return DeviceStatus.DeviceName; }
  10.     }
  11.  
  12.     public long TotalMemory {
  13.         get { return DeviceStatus.DeviceTotalMemory >> 20; }
  14.     }
  15.  
  16.     public PowerSource PowerSource {
  17.         get { return DeviceStatus.PowerSource; }
  18.     }
  19.  
  20.     public string DeviceManufacturer {
  21.         get { return DeviceStatus.DeviceManufacturer; }
  22.     }
  23. }

TotalMemory returns the result in megabytes, which is easier to use than the size in bytes.

Here’s a simple Grid that hosts some TextBlocks that bind to these properties:

  1. <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
  2.     <Grid.Resources>
  3.         <Style TargetType="TextBlock">
  4.             <Setter Property="FontSize" Value="24" />
  5.         </Style>
  6.     </Grid.Resources>
  7.     <Grid.RowDefinitions>
  8.         <RowDefinition Height="Auto" />
  9.         <RowDefinition Height="Auto" />
  10.         <RowDefinition Height="Auto" />
  11.         <RowDefinition Height="Auto" />
  12.         <RowDefinition Height="Auto" />
  13.     </Grid.RowDefinitions>
  14.     <Grid.ColumnDefinitions>
  15.         <ColumnDefinition Width="Auto" />
  16.         <ColumnDefinition Width="20" />
  17.         <ColumnDefinition />
  18.     </Grid.ColumnDefinitions>
  19.     <TextBlock Text="Device Name:" />
  20.     <TextBlock Grid.Column="2" Text="{Binding DeviceName}"/>
  21.     <TextBlock Grid.Row="1" Text="Device Type:" />
  22.     <TextBlock Grid.Row="1" Grid.Column="2" Text="{Binding DeviceType}"/>
  23.     <TextBlock Grid.Row="2" Text="Total Memory (MB):" />
  24.     <TextBlock Grid.Row="2" Grid.Column="2" Text="{Binding TotalMemory}"/>
  25.     <TextBlock Grid.Row="3" Text="Power Source:" />
  26.     <TextBlock Grid.Row="3" Grid.Column="2" Text="{Binding PowerSource}" />
  27.     <TextBlock Grid.Row="4" Text="Manufacturer:" />
  28.     <TextBlock Grid.Row="4" Grid.Column="2" Text="{Binding DeviceManufacturer}" />
  29. </Grid>

In the page constructor, the binding source is set through a DataContext:

  1. public MainPage() {
  2.     InitializeComponent();
  3.  
  4.     DataContext = new AppDeviceState();
  5. }

This is the result on the emulator:

image

And this is the result on my actual device:

image

The demo project is attached.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*