How to Use Cimbalino Windows Phone Toolkit UserInfo - UserExtendedPropertiesService

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 media library, location services and so on. Cimbalino.Phone.Toolkit.UserInfo is a MVVM compatible services for user information access.

The Cimbalino Toolkit's "UserExtendedPropertiesService" gets an anonymous identifier for the user of the device. This can be used, for example, to identify the device when it communicates with the web-service. The kit provides both the IUserExtendedPropertiesService interface and its implementation UserExtendedPropertiesService required to register the service in MVVM Light (note that MVVM and the MVVM Light Toolkit are not "preconditions" to use this service).

Building the example code

The source code for the code example is available here: UserExtendedPropertiesService 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_USER.

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.         if (!SimpleIoc.Default.IsRegistered<IUserExtendedPropertiesService>())  
  10.         {  
  11.              SimpleIoc.Default.Register<IUserExtendedPropertiesService, UserExtendedPropertiesService>();  
  12.          }  
  13.         SimpleIoc.Default.Register<MainViewModel>();  
  14.     }  
  15.   
  16.     public MainViewModel MainViewModel  
  17.     {  
  18.         get  
  19.         {  
  20.             return ServiceLocator.Current.GetInstance<MainViewModel>();  
  21.         }  
  22.     }  
  23.   
  24.     public static void Cleanup()  
  25.     {  
  26.         // TODO Clear the ViewModels  
  27.     }  
  28. }  
Implementing the ViewModel

Then we should implement the MainViewModel as in the following:
  1.    using Cimbalino.Phone.Toolkit.Services;  
  2.   
  3.   /// This class contains properties that the main View can data bind to.  
  4.   public class MainViewModel : ViewModelBase  
  5.   {  
  6.       /// The user extended properties service  
  7.       private readonly IUserExtendedPropertiesService _userExtendedPropertiesService;  
  8.   
  9.       /// Initializes a new instance of the MainViewModel class.  
  10.       public MainViewModel(IUserExtendedPropertiesService userExtendedPropertiesService)  
  11.       {  
  12.           _userExtendedPropertiesService = userExtendedPropertiesService;  
  13.       }  
  14.   
  15.       /// Gets the anonymous user identifier.  
  16.       public string AnonymousUserID  
  17.       {  
  18.           get  
  19.           {  
  20.               return _userExtendedPropertiesService.AnonymousUserID;  
  21.           }  
  22.       }  
  23.   }  
Implementing the View

Add the binding in the main page like:
  1. DataContext="{Binding MainViewModel, Source={StaticResource Locator}}"  
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.                             DataContext="{Binding MainViewModel,  
  9.                                                   Source={StaticResource Locator}}"  
  10.                             FontFamily="{StaticResource PhoneFontFamilyNormal}"  
  11.                             FontSize="{StaticResource PhoneFontSizeNormal}"  
  12.                             Foreground="{StaticResource PhoneForegroundBrush}"  
  13.                             Orientation="Portrait"  
  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.             <TextBlock Margin="9,-7,0,0"  
  33.                        Style="{StaticResource PhoneTextTitle2Style}"  
  34.                        Text="UserExtendedPropertiesService" />  
  35.         </StackPanel>  
  36.    
  37.         <!--  ContentPanel - place additional content here  -->  
  38.         <Grid x:Name="ContentPanel"  
  39.               Grid.Row="1"  
  40.               Margin="12,0,12,0" >  
  41.             <TextBlock TextWrapping="Wrap">Anonymous User ID : <Run Text="{Binding AnonymousUserID}"/></TextBlock>      
  42.         </Grid>  
  43.    
  44.     </Grid>  
  45.    
  46. </phone:PhoneApplicationPage>  

 

Up Next
    Ebook Download
    View all
    Learn
    View all