×
Namespaces

Variants
Actions

How to use Cimbalino Windows Phone Toolkit Device Info - DeviceExtendedPropertiesService

From Nokia Developer Wiki
Jump to: navigation, search

This article shows how to get the device extended properties using DeviceExtendedPropertiesService from the Cimbalino Windows Phone Toolkit.

SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Code ExampleTested with
SDK: Windows Phone 8.0 SDK, Windows Phone 7.1.1 SDK
Devices(s): Nokia Lumia 800, Nokia Lumia 920
CompatibilityPlatform Security
Capabilities: ID_CAP_IDENTITY_DEVICE
Article
Created: saramgsilva (11 Nov 2013)
Last edited: influencer (01 Feb 2014)

Contents

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 which deliver different features, ranging from base MVVM services and helpers, through to code for background agents, and for accessing media library, location services etc.

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).

This article explains how to use the service in an MVVM Light app. Developers should first read How to use MVVM Light Toolkit for Windows Phone for a general overview of MVVM Light and how to use services.

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. For more details see:


The sample is built using MVVM Light and the article does not go into great detail about the plumbing/infrastructure provided by the toolkit. For more information see:


Note.pngNote: 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 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:

<phone:PhoneApplicationPage x:Class="CimbalinoSample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
Orientation="Portrait"
DataContext="{Binding MainViewModel,
Source={StaticResource Locator}}"

SupportedOrientations="Portrait"
shell:SystemTray.IsVisible="True"
mc:Ignorable="d">
 
<!-- LayoutRoot is the root grid where all page content is placed -->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
 
<!-- TitlePanel contains the name of the application and page title -->
<StackPanel x:Name="TitlePanel"
Grid.Row="0"
Margin="12,17,0,28">
<TextBlock Margin="12,0"
Style="{StaticResource PhoneTextTitle2Style}"
Text="Cimbalino Sample" />
</StackPanel>
 
<!-- ContentPanel - place additional content here -->
<Grid x:Name="ContentPanel"
Grid.Row="1"
Margin="12,0,12,0">
<TextBlock TextWrapping="Wrap">
Device Unique ID: <LineBreak/><Run Text="{Binding DeviceUniqueID}" />
</TextBlock>
<TextBlock TextWrapping="Wrap" Margin="0,50,0,0">
Application Current Memory Usage: <LineBreak/><Run Text="{Binding ApplicationCurrentMemoryUsage}" />
</TextBlock>
<TextBlock TextWrapping="Wrap" Margin="0,100,0,0">
Application Peak Memory Usage: <LineBreak/><Run Text="{Binding ApplicationPeakMemoryUsage}" />
</TextBlock>
<TextBlock TextWrapping="Wrap" Margin="0,150,0,0">
Device Firmware Version: <LineBreak/><Run Text="{Binding DeviceFirmwareVersion}" />
</TextBlock>
<TextBlock TextWrapping="Wrap" Margin="0,200,0,0">
Device Hardware Version: <LineBreak/><Run Text="{Binding DeviceHardwareVersion}" />
</TextBlock>
<TextBlock TextWrapping="Wrap" Margin="0,250,0,0">
Device Manufacturer: <LineBreak/><Run Text="{Binding DeviceManufacturer}" />
</TextBlock>
<TextBlock TextWrapping="Wrap" Margin="0,300,0,0">
Device Name: <LineBreak/><Run Text="{Binding DeviceName}" />
</TextBlock>
<TextBlock TextWrapping="Wrap" Margin="0,350,0,0">
Device Total Memory: <LineBreak/><Run Text="{Binding DeviceTotalMemory}" />
</TextBlock>
<TextBlock TextWrapping="Wrap" Margin="0,400,0,0">
Original Mobile Operator Name: <LineBreak/><Run Text="{Binding OriginalMobileOperatorName}" />
</TextBlock>
</Grid>
</Grid>
 
</phone:PhoneApplicationPage>
This page was last modified on 1 February 2014, at 01:07.
374 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×