How to Use Cimbalino Windows Phone Toolkit Device Info - DeviceExtendedPropertiesService

Introduction

The Cimbalino Windows Phone Toolkit delivers a set of useful and powerful MVVM-compatible tools and services to help developers build Silverlight applications for Windows Phone. The Toolkit is divided in projects that deliver various features, ranging from base MVVM services and helpers, through to code for background agents and for accessing the media library, location services and so on.

The Cimbalino Toolkit's "Device Extended Properties" service is used to provide MVVM-compatible access to the properties in the system DeviceExtendedProperties class. The kit provides both the IDeviceExtendedPropertiesService interface and its implementation DeviceExtendedPropertiesService required to register the service in MVVM Light (note that MVVM and the MVVM Light Toolkit are not "preconditions" to use this service).

Cimbalino sample
A screenshot of the example app (from Simulator).

Building the example code

The source code for the code example is available here: DeviceExtendedPropertiesService Sample (Github).

To build the source code you will also need the MVVM Light Toolkit and the Cimbalino Windows Phone Toolkit. Their packages are available in the Nuget Package Manager.

Note: You must specify the following capabilities in the app manifest: ID_CAP_IDENTITY_DEVICE.

Registering the service

Register the service in the ViewModelLocator constructor as shown below:

  1. /// This class contains static references to all the view models in the  
  2. /// application and provides an entry point for the bindings.  
  3. public class ViewModelLocator  
  4. {  
  5.     /// Initializes a new instance of the ViewModelLocator class.  
  6.     public ViewModelLocator()  
  7.     {  
  8.         ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);  
  9.         Cimbalino.Phone.Toolkit.Services.DeviceExtendedPropertiesService  
  10.         if (!SimpleIoc.Default.IsRegistered<IDeviceExtendedPropertiesService>())  
  11.         {  
  12.             SimpleIoc.Default.Register<IDeviceExtendedPropertiesService, DeviceExtendedPropertiesService>();  
  13.         }  
  14.   
  15.         SimpleIoc.Default.Register<MainViewModel>();  
  16.     }  
  17.   
  18.     public MainViewModel MainViewModel  
  19.     {  
  20.         get  
  21.         {  
  22.             return ServiceLocator.Current.GetInstance<MainViewModel>();  
  23.         }  
  24.     }  
  25.   
  26.     public static void Cleanup()  
  27.     {  
  28.         // TODO Clear the ViewModels  
  29.     }  
  30. }  
Implementing the ViewModel

Implement the MainViewModel as shown below. The highlighted sections show the MainViewModel constructor taking the IDeviceExtendedPropertiesService parameter and assigning it to the private variable and later on the variable being used to access the device information.
  1. /// This class contains properties that the main View can data bind to.  
  2. public class MainViewModel : ViewModelBase  
  3. {  
  4.     /// The device extended properties service  
  5.     private readonly IDeviceExtendedPropertiesService _deviceExtendedPropertiesService;  
  6.   
  7.     /// Initializes a new instance of the MainViewModel class.  
  8.     public MainViewModel(IDeviceExtendedPropertiesService deviceExtendedPropertiesService)  
  9.     {  
  10.         _deviceExtendedPropertiesService = deviceExtendedPropertiesService;  
  11.     }  
  12.   
  13.     /// Gets the device unique identifier.  
  14.     public string DeviceUniqueID  
  15.     {  
  16.         get  
  17.         {  
  18.             return Convert.ToBase64String(_deviceExtendedPropertiesService.DeviceUniqueId);  
  19.         }  
  20.     }  
  21.   
  22.   
  23.     /// Gets the application current memory usage.  
  24.     public long ApplicationCurrentMemoryUsage  
  25.     {  
  26.         get  
  27.         {  
  28.             return _deviceExtendedPropertiesService.GetDeviceProperty<long>("ApplicationCurrentMemoryUsage");  
  29.         }  
  30.     }  
  31.   
  32.   
  33.     /// Gets the application peak memory usage.  
  34.     public long ApplicationPeakMemoryUsage  
  35.     {  
  36.         get  
  37.         {  
  38.             return _deviceExtendedPropertiesService.GetDeviceProperty<long>("ApplicationPeakMemoryUsage");  
  39.         }  
  40.     }  
  41.   
  42.   
  43.     /// Gets the device firmware version.  
  44.     public string DeviceFirmwareVersion  
  45.     {  
  46.         get  
  47.         {  
  48.             return _deviceExtendedPropertiesService.GetDeviceProperty<string>("DeviceFirmwareVersion");  
  49.         }  
  50.     }  
  51.   
  52.   
  53.     /// Gets the device hardware version.  
  54.     public string DeviceHardwareVersion  
  55.     {  
  56.         get  
  57.         {  
  58.             return _deviceExtendedPropertiesService.GetDeviceProperty<string>("DeviceHardwareVersion");  
  59.         }  
  60.     }  
  61.   
  62.   
  63.     /// Gets the device manufacturer.  
  64.     public string DeviceManufacturer  
  65.     {  
  66.         get  
  67.         {  
  68.             return _deviceExtendedPropertiesService.GetDeviceProperty<string>("DeviceManufacturer");  
  69.         }  
  70.     }  
  71.   
  72.   
  73.     /// Gets the name of the device.  
  74.     public string DeviceName  
  75.     {  
  76.         get  
  77.         {  
  78.             return _deviceExtendedPropertiesService.GetDeviceProperty<string>("DeviceName");  
  79.         }  
  80.     }  
  81.   
  82.     /// Gets the device total memory.  
  83.     public long DeviceTotalMemory  
  84.     {  
  85.         get  
  86.         {  
  87.             return _deviceExtendedPropertiesService.GetDeviceProperty<long>("DeviceTotalMemory");  
  88.         }  
  89.     }  
  90.   
  91.   
  92.     /// Gets the name of the original mobile operator.  
  93.     public string OriginalMobileOperatorName  
  94.     {  
  95.         get  
  96.         {  
  97.             return _deviceExtendedPropertiesService.GetDeviceProperty<string>("OriginalMobileOperatorName");  
  98.         }  
  99.     }  
  100. }  
Implementing the view

The MainPage.xaml can be the following:
  1. <phone:PhoneApplicationPage x:Class="CimbalinoSample.MainPage"  
  2.                             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.                             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.                             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  5.                             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  6.                             xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"  
  7.                             xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"  
  8.                             FontFamily="{StaticResource PhoneFontFamilyNormal}"  
  9.                             FontSize="{StaticResource PhoneFontSizeNormal}"  
  10.                             Foreground="{StaticResource PhoneForegroundBrush}"  
  11.                             Orientation="Portrait"  
  12.                              DataContext="{Binding MainViewModel,   
  13.                                                     Source={StaticResource Locator}}"  
  14.                             SupportedOrientations="Portrait"  
  15.                             shell:SystemTray.IsVisible="True"  
  16.                             mc:Ignorable="d">  
  17.    
  18.     <!--  LayoutRoot is the root grid where all page content is placed  -->  
  19.     <Grid x:Name="LayoutRoot" Background="Transparent">  
  20.         <Grid.RowDefinitions>  
  21.             <RowDefinition Height="Auto" />  
  22.             <RowDefinition Height="*" />  
  23.         </Grid.RowDefinitions>  
  24.    
  25.         <!--  TitlePanel contains the name of the application and page title  -->  
  26.         <StackPanel x:Name="TitlePanel"  
  27.                     Grid.Row="0"  
  28.                     Margin="12,17,0,28">  
  29.             <TextBlock Margin="12,0"  
  30.                        Style="{StaticResource PhoneTextTitle2Style}"  
  31.                        Text="Cimbalino Sample" />  
  32.         </StackPanel>  
  33.    
  34.         <!--  ContentPanel - place additional content here  -->  
  35.         <Grid x:Name="ContentPanel"  
  36.               Grid.Row="1"  
  37.               Margin="12,0,12,0">  
  38.             <TextBlock TextWrapping="Wrap">  
  39.                 Device Unique ID: <LineBreak/><Run Text="{Binding DeviceUniqueID}" />  
  40.             </TextBlock>  
  41.             <TextBlock TextWrapping="Wrap" Margin="0,50,0,0">  
  42.                 Application Current Memory Usage: <LineBreak/><Run Text="{Binding ApplicationCurrentMemoryUsage}" />  
  43.             </TextBlock>  
  44.             <TextBlock TextWrapping="Wrap" Margin="0,100,0,0">  
  45.                 Application Peak Memory Usage: <LineBreak/><Run Text="{Binding ApplicationPeakMemoryUsage}" />  
  46.             </TextBlock>  
  47.             <TextBlock TextWrapping="Wrap" Margin="0,150,0,0">  
  48.                Device Firmware Version: <LineBreak/><Run Text="{Binding DeviceFirmwareVersion}" />  
  49.             </TextBlock>  
  50.             <TextBlock TextWrapping="Wrap" Margin="0,200,0,0">  
  51.                 Device Hardware Version: <LineBreak/><Run Text="{Binding DeviceHardwareVersion}" />  
  52.             </TextBlock>  
  53.             <TextBlock TextWrapping="Wrap" Margin="0,250,0,0">  
  54.                 Device Manufacturer: <LineBreak/><Run Text="{Binding DeviceManufacturer}" />  
  55.             </TextBlock>  
  56.             <TextBlock TextWrapping="Wrap" Margin="0,300,0,0">  
  57.                 Device Name: <LineBreak/><Run Text="{Binding DeviceName}" />  
  58.             </TextBlock>  
  59.             <TextBlock TextWrapping="Wrap" Margin="0,350,0,0">  
  60.                 Device Total Memory: <LineBreak/><Run Text="{Binding DeviceTotalMemory}" />  
  61.             </TextBlock>  
  62.             <TextBlock TextWrapping="Wrap" Margin="0,400,0,0">  
  63.                Original Mobile Operator Name: <LineBreak/><Run Text="{Binding OriginalMobileOperatorName}" />  
  64.             </TextBlock>  
  65.         </Grid>  
  66.     </Grid>  
  67.    
  68. </phone:PhoneApplicationPage>  


Similar Articles