How to Use Cimbalino Windows Phone Toolkit To Read Manifest File (Using MVVM)

This code example shows how to read information in the application manifest file (AppManifest.xml) using ApplicationManifestService from the Cimbalino Windows Phone Toolkit .

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 into projects that deliver various features, ranging from base MVVM services and helpers, through to code for background agents and for accessing a media library, location services and so on. The base project (Cimbalino.Phone.Toolkit ) contains base MVVM services, some very useful converters, helper classes and extension methods.

Cimbalino.Phone.Toolkit.Background (contains the ApplicationManifestService) is MVVM compatible services for background agents.

The Cimbalino Toolkit's Application Manifest service is used to read information from the application manifest. This file includes, in particular, information that it can be useful to display in an app's about page, like the app version, author, product ID and so on. The kit provides both the IApplicationManifestService interface and its implementation ApplicationManifestService required to register the service in MVVM Light (note that MVVM and the MVVM Light Toolkit are not "preconditions" to use this service).

A screenshot of the example app is shown below:

Cimbalino Sample in Windows Phone

Application manifest file information displayed in the app.

Building the Example Code

The source code for the code example is available here: ApplicationManifestService (Include sample for WP8.0 and WP8.1SL).

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 .

The Sample

Registering the service

We should register each service in ViewModelLocator, as in the following:

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

Implementing the ViewModel

Then we should implement the MainViewModel as in the following:

  1. /// <summary>  
  2. /// This class contains properties that the main View can data bind to.  
  3. /// </summary>  
  4. public class MainViewModel : ViewModelBase  
  5. {  
  6.     /// <summary>  
  7.     /// The public application url.  
  8.     /// </summary>  
  9.     private readonly string _appUrl;  
  10.   
  11.     /// <summary>  
  12.     /// The application manifest.  
  13.     /// </summary>  
  14.     private readonly ApplicationManifest _applicationManifest;  
  15.   
  16.     /// <summary>  
  17.     /// Initializes a new instance of the <see cref="MainViewModel"/> class.  
  18.     /// </summary>  
  19.     /// <param name="emailComposeService">  
  20.     /// The email Compose Service.  
  21.     /// </param>  
  22.     /// <param name="applicationManifestService">  
  23.     /// The application Manifest Service.  
  24.     /// </param>  
  25.     /// <param name="marketplaceReviewService">  
  26.     /// The marketplace review service  
  27.     /// </param>  
  28.     /// <param name="shareLinkService">  
  29.     /// The share Link Service.  
  30.     /// </param>  
  31.     public MainViewModel(IApplicationManifestService applicationManifestService)  
  32.     {  
  33.         _applicationManifest = applicationManifestService.GetApplicationManifest();  
  34.         _appUrl = string.Concat("http://windowsphone.com/s?appid= ", _applicationManifest.App.ProductId);  
  35.     }  
  36.   
  37.     /// <summary>  
  38.     /// Gets the title.  
  39.     /// </summary>  
  40.     public string Title  
  41.     {  
  42.         get  
  43.         {  
  44.             return _applicationManifest.App.Title;  
  45.         }  
  46.     }  
  47.   
  48.     /// <summary>  
  49.     /// Gets the author.  
  50.     /// </summary>  
  51.     public string Author  
  52.     {  
  53.         get  
  54.         {  
  55.             return _applicationManifest.App.Author;  
  56.         }  
  57.     }  
  58.   
  59.     /// <summary>  
  60.     /// Gets the version.  
  61.     /// </summary>  
  62.     public string Version  
  63.     {  
  64.         get  
  65.         {  
  66.             return _applicationManifest.App.Version;  
  67.         }  
  68.     }  
  69.   
  70.     /// <summary>  
  71.     /// Gets the description.  
  72.     /// </summary>  
  73.     public string Description  
  74.     {  
  75.         get  
  76.         {  
  77.             return _applicationManifest.App.Description;  
  78.         }  
  79.     }  
  80.   
  81.     /// <summary>  
  82.     /// Gets the product ID.  
  83.     /// </summary>  
  84.     public string ProductID  
  85.     {  
  86.         get  
  87.         {  
  88.             return _applicationManifest.App.ProductId;  
  89.         }  
  90.     }  
  91.   
  92.     /// <summary>  
  93.     /// Gets the publisher.  
  94.     /// </summary>  
  95.     public string Publisher  
  96.     {  
  97.         get  
  98.         {  
  99.             return _applicationManifest.App.Publisher;  
  100.         }  
  101.     }  
  102.   
  103.     /// <summary>  
  104.     /// Gets the capabilities.  
  105.     /// </summary>  
  106.     public IList Capabilities  
  107.     {  
  108.         get  
  109.         {  
  110.             return _applicationManifest.App.Capabilities.ToList();  
  111.         }  
  112.     }  

Implementing the View

Add the binding to the main page as shown:

  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" Grid.Row="0" Margin="12,17,0,28">  
  27.             <TextBlock Margin="12,0" Style="{StaticResource PhoneTextTitle2Style}" Text="Cimbalino Sample" />  
  28.         </StackPanel>  
  29.   
  30.         <!-- ContentPanel - place additional content here -->  
  31.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" >  
  32.             <TextBlock TextWrapping="Wrap" VerticalAlignment="Top" Height="50">  
  33.                 Title: <Run Text="{Binding Title}"/>  
  34.             </TextBlock>  
  35.             <TextBlock TextWrapping="Wrap" VerticalAlignment="Top" Margin="0,50,0,0">  
  36.                 Author: <Run Text="{Binding Author}"/>  
  37.             </TextBlock>  
  38.             <TextBlock TextWrapping="Wrap" VerticalAlignment="Top" Margin="0,100,0,0">  
  39.                 Version: <Run Text="{Binding Version}"/>  
  40.             </TextBlock>  
  41.             <TextBlock TextWrapping="Wrap" VerticalAlignment="Top" Margin="0,150,0,0">  
  42.                 Description: <Run Text="{Binding Description}"/>  
  43.             </TextBlock>  
  44.             <TextBlock TextWrapping="Wrap" VerticalAlignment="Top" Margin="0,300,0,0">  
  45.                 ProductID: <Run Text="{Binding ProductID}"/>  
  46.             </TextBlock>  
  47.             <TextBlock TextWrapping="Wrap" VerticalAlignment="Top" Margin="0,350,0,0">  
  48.                 Publisher: <Run Text="{Binding Publisher}"/>  
  49.             </TextBlock>  
  50.             <TextBlock TextWrapping="Wrap" Text="Capabilities:" VerticalAlignment="Top" Margin="0,400,0,0"/>  
  51.             <ListBox DisplayMemberPath="Name" Margin="0,450,0,0" ItemsSource="{Binding Capabilities}"/>  
  52.         </Grid>  
  53.     </Grid>  
  54. </phone:PhoneApplicationPage> 

Source Code Files

  • ViewModelLocator.cs has the ViewModelLocator class that helps to bind the view model with the view.
  • MainViewModel.cs has the MainViewModel class that represents the view model for the main page.
  • MainPage.xaml and MainPage.xaml.cs represent the main page.

See Also


Similar Articles