×
Namespaces

Variants
Actions
(Difference between revisions)

How to use Cimbalino Windows Phone Toolkit - ApplicationManifestService

From Nokia Developer Wiki
Jump to: navigation, search
saramgsilva (Talk | contribs)
(Saramgsilva - - Introduction)
hamishwillee (Talk | contribs)
m (Hamishwillee - Tidy example code)
Line 24: Line 24:
 
== Introduction ==
 
== Introduction ==
  
The ''Cimbalino Windows Phone Toolkit'' delivers a set of useful and powerful [http://en.wikipedia.org/wiki/Model_View_ViewModel 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 ([http://github.com/Cimbalino/Cimbalino-Phone-Toolkit Cimbalino.Phone.Toolkit]) contains base MVVM services, some very useful converters, helper classes and extension methods. Cimbalino.Phone.Toolkit.Background is a MVVM compatible services for background agents.
+
The ''Cimbalino Windows Phone Toolkit'' delivers a set of useful and powerful [http://en.wikipedia.org/wiki/Model_View_ViewModel 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 ([http://github.com/Cimbalino/Cimbalino-Phone-Toolkit Cimbalino.Phone.Toolkit]) contains base MVVM services, some very useful converters, helper classes and extension methods. '''Cimbalino.Phone.Toolkit.Background''' (contains the {{Icode|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 [http://github.com/Cimbalino/Cimbalino-Phone-Toolkit/blob/master/src/Cimbalino.Phone.Toolkit.Background%20%28WP71%29/Services/IApplicationManifestService.cs IApplicationManifestService] interface and its implementation [http://github.com/Cimbalino/Cimbalino-Phone-Toolkit/blob/master/src/Cimbalino.Phone.Toolkit.Background%20%28WP71%29/Services/ApplicationManifestService.cs ApplicationManifestService] required to [[How to use MVVM Light Toolkit for Windows Phone#How to use services|register the service in MVVM Light]] (note that MVVM and the MVVM Light Toolkit are not "preconditions" to use this service).  
 
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 [http://github.com/Cimbalino/Cimbalino-Phone-Toolkit/blob/master/src/Cimbalino.Phone.Toolkit.Background%20%28WP71%29/Services/IApplicationManifestService.cs IApplicationManifestService] interface and its implementation [http://github.com/Cimbalino/Cimbalino-Phone-Toolkit/blob/master/src/Cimbalino.Phone.Toolkit.Background%20%28WP71%29/Services/ApplicationManifestService.cs ApplicationManifestService] required to [[How to use MVVM Light Toolkit for Windows Phone#How to use services|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 [http://mvvmlight.codeplex.com/ MVVM Light] app. Developers should first read [[How to use MVVM Light Toolkit for Windows Phone#How to use services|How to use MVVM Light Toolkit for Windows Phone]] for a general overview of MVVM Light and how to use services.
 
This article explains how to use the service in an [http://mvvmlight.codeplex.com/ MVVM Light] app. Developers should first read [[How to use MVVM Light Toolkit for Windows Phone#How to use services|How to use MVVM Light Toolkit for Windows Phone]] for a general overview of MVVM Light and how to use services.
Line 49: Line 48:
 
== Registering the service ==
 
== Registering the service ==
  
We should register each service in ViewModelLocator, as following:
+
We should register each service in {{Icode|ViewModelLocator}}, as following:
  
<code csharp>
+
<code csharp 1 highlight="10-13">
/// <summary>
+
 
     /// This class contains static references to all the view models in the
 
     /// This class contains static references to all the view models in the
 
     /// application and provides an entry point for the bindings.
 
     /// application and provides an entry point for the bindings.
    /// </summary>
 
 
     public class ViewModelLocator
 
     public class ViewModelLocator
 
     {
 
     {
        /// <summary>
 
 
         /// Initializes a new instance of the ViewModelLocator class.
 
         /// Initializes a new instance of the ViewModelLocator class.
        /// </summary>
 
 
         public ViewModelLocator()
 
         public ViewModelLocator()
 
         {
 
         {
Line 91: Line 86:
 
Then we should implement the MainViewModel as following:
 
Then we should implement the MainViewModel as following:
  
<code csharp>
+
<code csharp 1 highlight="8,11-15">
/// <summary>
+
 
     /// This class contains properties that the main View can data bind to.
 
     /// This class contains properties that the main View can data bind to.
    /// </summary>
 
 
     public class MainViewModel : ViewModelBase
 
     public class MainViewModel : ViewModelBase
 
     {
 
     {
        /// <summary>
 
 
         /// The public application url.
 
         /// The public application url.
        /// </summary>
 
 
         private readonly string _appUrl;
 
         private readonly string _appUrl;
  
        /// <summary>
 
 
         /// The application manifest.
 
         /// The application manifest.
        /// </summary>
 
 
         private readonly ApplicationManifest _applicationManifest;
 
         private readonly ApplicationManifest _applicationManifest;
  
        /// <summary>
+
         /// Initializes a new instance of the Application Manifest Service.
         /// Initializes a new instance of the <see cref="MainViewModel"/> class.
+
        /// </summary>
+
        /// <param name="emailComposeService">
+
        /// The email Compose Service.
+
        /// </param>
+
        /// <param name="applicationManifestService">
+
        /// The application Manifest Service.
+
        /// </param>
+
        /// <param name="marketplaceReviewService">
+
        /// The marketplace review service
+
        /// </param>
+
        /// <param name="shareLinkService">
+
        /// The share Link Service.
+
        /// </param>
+
 
         public MainViewModel(IApplicationManifestService applicationManifestService)
 
         public MainViewModel(IApplicationManifestService applicationManifestService)
 
         {
 
         {
Line 128: Line 103:
 
         }
 
         }
  
        /// <summary>
 
 
         /// Gets the title.
 
         /// Gets the title.
        /// </summary>
 
 
         public string Title
 
         public string Title
 
         {
 
         {
Line 139: Line 112:
 
         }
 
         }
  
        /// <summary>
 
 
         /// Gets the author.
 
         /// Gets the author.
        /// </summary>
 
 
         public string Author
 
         public string Author
 
         {
 
         {
Line 150: Line 121:
 
         }
 
         }
  
        /// <summary>
 
 
         /// Gets the version.
 
         /// Gets the version.
        /// </summary>
 
 
         public string Version
 
         public string Version
 
         {
 
         {
Line 161: Line 130:
 
         }
 
         }
  
        /// <summary>
 
 
         /// Gets the description.
 
         /// Gets the description.
        /// </summary>
 
 
         public string Description
 
         public string Description
 
         {
 
         {
Line 172: Line 139:
 
         }
 
         }
  
        /// <summary>
 
 
         /// Gets the product ID.
 
         /// Gets the product ID.
        /// </summary>
 
 
         public string ProductID
 
         public string ProductID
 
         {
 
         {
Line 183: Line 148:
 
         }
 
         }
  
        /// <summary>
 
 
         /// Gets the publisher.
 
         /// Gets the publisher.
        /// </summary>
 
 
         public string Publisher
 
         public string Publisher
 
         {
 
         {
Line 194: Line 157:
 
         }
 
         }
  
        /// <summary>
 
 
         /// Gets the capabilities.
 
         /// Gets the capabilities.
        /// </summary>
 
 
         public IList Capabilities
 
         public IList Capabilities
 
         {
 
         {
Line 209: Line 170:
 
== Implementing the view ==
 
== Implementing the view ==
  
and add the binding in main page like:  
+
Add the binding in main page as shown:  
  
 
<code xml>
 
<code xml>
Line 216: Line 177:
 
</code>
 
</code>
  
The MainPage.xaml can be the following:
+
The '''MainPage.xaml''' can be the following:
  
<code xml>
+
<code xml 1 highlight="8-9">
 
<phone:PhoneApplicationPage x:Class="CimbalinoSample.MainPage"
 
<phone:PhoneApplicationPage x:Class="CimbalinoSample.MainPage"
 
                             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 
                             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

Revision as of 03:43, 19 November 2013

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: hamishwillee (19 Nov 2013)

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.

108 page views in the last 30 days.
×