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:
-
-
- public class ViewModelLocator
- {
-
- public ViewModelLocator()
- {
- ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
- if (!SimpleIoc.Default.IsRegistered<IUserExtendedPropertiesService>())
- {
- SimpleIoc.Default.Register<IUserExtendedPropertiesService, UserExtendedPropertiesService>();
- }
- SimpleIoc.Default.Register<MainViewModel>();
- }
-
- public MainViewModel MainViewModel
- {
- get
- {
- return ServiceLocator.Current.GetInstance<MainViewModel>();
- }
- }
-
- public static void Cleanup()
- {
-
- }
- }
Implementing the ViewModel
Then we should implement the MainViewModel as in the following:
- using Cimbalino.Phone.Toolkit.Services;
-
-
- public class MainViewModel : ViewModelBase
- {
-
- private readonly IUserExtendedPropertiesService _userExtendedPropertiesService;
-
-
- public MainViewModel(IUserExtendedPropertiesService userExtendedPropertiesService)
- {
- _userExtendedPropertiesService = userExtendedPropertiesService;
- }
-
-
- public string AnonymousUserID
- {
- get
- {
- return _userExtendedPropertiesService.AnonymousUserID;
- }
- }
- }
Implementing the View
Add the binding in the main page like:
- DataContext="{Binding MainViewModel, Source={StaticResource Locator}}"
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"
- DataContext="{Binding MainViewModel,
- Source={StaticResource Locator}}"
- FontFamily="{StaticResource PhoneFontFamilyNormal}"
- FontSize="{StaticResource PhoneFontSizeNormal}"
- Foreground="{StaticResource PhoneForegroundBrush}"
- Orientation="Portrait"
- 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" />
- <TextBlock Margin="9,-7,0,0"
- Style="{StaticResource PhoneTextTitle2Style}"
- Text="UserExtendedPropertiesService" />
- </StackPanel>
-
- <!-- ContentPanel - place additional content here -->
- <Grid x:Name="ContentPanel"
- Grid.Row="1"
- Margin="12,0,12,0" >
- <TextBlock TextWrapping="Wrap">Anonymous User ID : <Run Text="{Binding AnonymousUserID}"/></TextBlock>
- </Grid>
-
- </Grid>
-
- </phone:PhoneApplicationPage>