How to Add Marketplace Review Using the Cimbalino Windows Phone Toolkit (Using MVVM)

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 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.

The Cimbalino Toolkit's "MarketplaceReview" service is used to launch the marketplace review screen for an app, making it easier for users to rate and review. The kit provides both the IMarketplaceReviewService interface and its implementation MarketplaceReviewService 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, with the icon used to launch a marketplace review, is shown below:

Cimbalino Toolkit Sample in Windows Phone

Building the Example Code

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

The Sample

Registering the Service

Register the service in the ViewModelLocator constructor as shown below (ViewModelLocator.cs ).
  1. public ViewModelLocator()  
  2. {  
  3.     ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);  
  4.     if (!SimpleIoc.Default.IsRegistered<IMarketplaceReviewService>())  
  5.     {  
  6.         SimpleIoc.Default.Register<IMarketplaceReviewService, MarketplaceReviewService>();  
  7.     }  
  8.     SimpleIoc.Default.Register<MainViewModel>();  

In the next section we see that the MainViewModel constructor takes an IMarketplaceReviewService parameter. When the ViewModelLocator creates the view model it recognises that the parameter is registered, creates an instance of the MarketplaceReviewService and passes it to the MainViewModel.

Implementing the ViewModel

Implement the MainViewModel as shown below. The highlighted sections show the MainViewModel constructor taking theIMarketplaceReviewService parameter and assigning it to the private variable and later on the variable being used to show the marketplace review prompt.
  1. /// This class contains properties that the main View can data bind to.  
  2. public class MainViewModel : ViewModelBase  
  3. {  
  4.     /// The marketplace review service.  
  5.     private readonly IMarketplaceReviewService _marketplaceReviewService;  
  6.     /// The public application url.  
  7.     private readonly string _appUrl;  
  8.       
  9.     /// The marketplace review service  
  10.     public MainViewModel(IMarketplaceReviewService marketplaceReviewService)  
  11.     {  
  12.         _marketplaceReviewService = marketplaceReviewService;  
  13.         RateCommand = new RelayCommand(Rate);  
  14.     }  
  15.   
  16.     /// Gets the rate command.  
  17.     public ICommand RateCommand { getprivate set; }  
  18.   
  19.     /// The rate.  
  20.     private void Rate()  
  21.     {  
  22.         _marketplaceReviewService.Show();  
  23.     }  

Implementing the View

The rest of the app is "plumbing" to hook up the MainViewModel to the View and send commands back to the invoke the service.
  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:cimbalino="clr-namespace:Cimbalino.Phone.Toolkit.Behaviors;assembly=Cimbalino.Phone.Toolkit"  
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008 "  
  6. xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"  
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006 "  
  8. xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"  
  9. xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"  
  10. DataContext="{Binding MainViewModel,  
  11. Source={StaticResource Locator}}"  
  12. FontFamily="{StaticResource PhoneFontFamilyNormal}"  
  13. FontSize="{StaticResource PhoneFontSizeNormal}"  
  14. Foreground="{StaticResource PhoneForegroundBrush}"  
  15. Orientation="Portrait"  
  16. SupportedOrientations="Portrait"  
  17. shell:SystemTray.IsVisible="True"  
  18. mc:Ignorable="d">  
  19.     <!-- LayoutRoot is the root grid where all page content is placed -->  
  20.         <Grid x:Name="LayoutRoot" Background="Transparent">  
  21.             <Grid.RowDefinitions>  
  22.                 <RowDefinition Height="Auto" />  
  23.                 <RowDefinition Height="*" />  
  24.             </Grid.RowDefinitions>  
  25.         <!-- TitlePanel contains the name of the application and page title -->  
  26.         <StackPanel x:Name="TitlePanel" Grid.Row="1" Margin="0,5,12,396">  
  27.             <TextBlock Margin="12,0" Style="{StaticResource PhoneTextTitle2Style}" Text="Cimbalino Toolkit Sample" />  
  28.         </StackPanel>  
  29.             <TextBlock Grid.RowSpan="2" Margin="12,50,-3,487" Style="{StaticResource PhoneTextTitle3Style}" TextWrapping="Wrap">  
  30.                 This samples has the goal to show how to use Cimbalino Toolkit - MarketplaceReviewService  
  31.             </TextBlock>  
  32.         <!-- ContentPanel - place additional content here -->  
  33.             <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" />  
  34.                 <i:Interaction.Behaviors>  
  35.                     <cimbalino:ApplicationBarBehavior>  
  36.                         <cimbalino:ApplicationBarIconButton Command="{Binding RateCommand, Mode=OneTime}" IconUri="/Images/appbar.rate.png" Text="Rate it" />  
  37.                         </cimbalino:ApplicationBarBehavior>  
  38.                     </i:Interaction.Behaviors>  
  39.         </Grid>  
  40. </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 main page.
  • MainPage.xaml and MainPage.xaml.cs represent the main page.

See Also

Next Recommended Readings