×
Namespaces

Variants
Actions

How to use Cimbalino Windows Phone Toolkit - ApplicationManifestService

From Nokia Developer Wiki
Jump to: navigation, search

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

WP Metro Icon File.png
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, 920
CompatibilityArticle
Created: saramgsilva (05 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 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 a 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 etc. 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).

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 is shown below.

Application manifest file information displayed in app

Building the example code

The source code for the code example is available here: ApplicationManifestService (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:

Registering the service

We should register each service in ViewModelLocator, as following:

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

Implementing the ViewModel

Then we should implement the MainViewModel as following:

  1.     /// This class contains properties that the main View can data bind to.
  2.     public class MainViewModel : ViewModelBase
  3.     {
  4.         /// The public application url.
  5.         private readonly string _appUrl;
  6.  
  7.         /// The application manifest.
  8.         private readonly ApplicationManifest _applicationManifest;
  9.  
  10.         /// Initializes a new instance of the Application Manifest Service.
  11.         public MainViewModel(IApplicationManifestService applicationManifestService)
  12.         {
  13.             _applicationManifest = applicationManifestService.GetApplicationManifest();
  14.             _appUrl = string.Concat("http://windowsphone.com/s?appid=", _applicationManifest.App.ProductId);
  15.         }
  16.  
  17.         /// Gets the title.
  18.         public string Title
  19.         {
  20.             get
  21.             {
  22.                 return _applicationManifest.App.Title;
  23.             }
  24.         }
  25.  
  26.         /// Gets the author.
  27.         public string Author
  28.         {
  29.             get
  30.             {
  31.                 return _applicationManifest.App.Author;
  32.             }
  33.         }
  34.  
  35.         /// Gets the version.
  36.         public string Version
  37.         {
  38.             get
  39.             {
  40.                 return _applicationManifest.App.Version;
  41.             }
  42.         }
  43.  
  44.         /// Gets the description.
  45.         public string Description
  46.         {
  47.             get
  48.             {
  49.                 return _applicationManifest.App.Description;
  50.             }
  51.         }
  52.  
  53.         /// Gets the product ID.
  54.         public string ProductID
  55.         {
  56.             get
  57.             {
  58.                 return _applicationManifest.App.ProductId;
  59.             }
  60.         }
  61.  
  62.         /// Gets the publisher.
  63.         public string Publisher
  64.         {
  65.             get
  66.             {
  67.                 return _applicationManifest.App.Publisher;
  68.             }
  69.         }
  70.  
  71.         /// Gets the capabilities.
  72.         public IList Capabilities
  73.         {
  74.             get
  75.             {
  76.                 return _applicationManifest.App.Capabilities.ToList();
  77.             }
  78.         }
  79.     }

Implementing the view

Add the binding in main page as shown:

 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.         </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.                        VerticalAlignment="Top" Height="50">
  40.                 Title: <Run Text="{Binding Title}"/>
  41.                 </TextBlock>
  42.             <TextBlock TextWrapping="Wrap" 
  43.                        VerticalAlignment="Top" Margin="0,50,0,0">
  44.                 Author: <Run Text="{Binding Author}"/>
  45.             </TextBlock>
  46.             <TextBlock TextWrapping="Wrap" 
  47.                        VerticalAlignment="Top" Margin="0,100,0,0">
  48.                 Version: <Run Text="{Binding Version}"/>
  49.             </TextBlock>
  50.             <TextBlock TextWrapping="Wrap" 
  51.                        VerticalAlignment="Top" Margin="0,150,0,0">
  52.                 Description: <Run Text="{Binding Description}"/>
  53.             </TextBlock>
  54.             <TextBlock TextWrapping="Wrap"
  55.                        VerticalAlignment="Top" Margin="0,300,0,0">
  56.                 ProductID: <Run Text="{Binding ProductID}"/>
  57.             </TextBlock>
  58.             <TextBlock TextWrapping="Wrap"
  59.                        VerticalAlignment="Top" Margin="0,350,0,0">
  60.                 Publisher: <Run Text="{Binding Publisher}"/>
  61.             </TextBlock>
  62.             <TextBlock TextWrapping="Wrap" Text="Capabilities:" 
  63.                        VerticalAlignment="Top" Margin="0,400,0,0"/>
  64.             <ListBox DisplayMemberPath="Name" Margin="0,450,0,0"
  65.                      ItemsSource="{Binding Capabilities}"/>
  66.         </Grid>
  67.  
  68.     </Grid>
  69.  
  70. </phone:PhoneApplicationPage>


Related Samples

Source Code

The source code is available here.

This page was last modified on 1 February 2014, at 01:04.
164 page views in the last 30 days.