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:
Building the Example CodeThe 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 ServiceRegister the service in the ViewModelLocator constructor as shown below (ViewModelLocator.cs ).
- public ViewModelLocator()
- {
- ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
- if (!SimpleIoc.Default.IsRegistered<IMarketplaceReviewService>())
- {
- SimpleIoc.Default.Register<IMarketplaceReviewService, MarketplaceReviewService>();
- }
- 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 ViewModelImplement 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.
-
- public class MainViewModel : ViewModelBase
- {
-
- private readonly IMarketplaceReviewService _marketplaceReviewService;
-
- private readonly string _appUrl;
-
-
- public MainViewModel(IMarketplaceReviewService marketplaceReviewService)
- {
- _marketplaceReviewService = marketplaceReviewService;
- RateCommand = new RelayCommand(Rate);
- }
-
-
- public ICommand RateCommand { get; private set; }
-
-
- private void Rate()
- {
- _marketplaceReviewService.Show();
- }
- }
Implementing the ViewThe rest of the app is "plumbing" to hook up the MainViewModel to the View and send commands back to the invoke the service.
- <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:cimbalino="clr-namespace:Cimbalino.Phone.Toolkit.Behaviors;assembly=Cimbalino.Phone.Toolkit"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008 "
- xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
- 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">
-
- <Grid x:Name="LayoutRoot" Background="Transparent">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto" />
- <RowDefinition Height="*" />
- </Grid.RowDefinitions>
-
- <StackPanel x:Name="TitlePanel" Grid.Row="1" Margin="0,5,12,396">
- <TextBlock Margin="12,0" Style="{StaticResource PhoneTextTitle2Style}" Text="Cimbalino Toolkit Sample" />
- </StackPanel>
- <TextBlock Grid.RowSpan="2" Margin="12,50,-3,487" Style="{StaticResource PhoneTextTitle3Style}" TextWrapping="Wrap">
- This samples has the goal to show how to use Cimbalino Toolkit - MarketplaceReviewService
- </TextBlock>
-
- <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" />
- <i:Interaction.Behaviors>
- <cimbalino:ApplicationBarBehavior>
- <cimbalino:ApplicationBarIconButton Command="{Binding RateCommand, Mode=OneTime}" IconUri="/Images/appbar.rate.png" Text="Rate it" />
- </cimbalino:ApplicationBarBehavior>
- </i:Interaction.Behaviors>
- </Grid>
- </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