How to Use Cimbalino Windows Phone Toolkit - MessageBoxService

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 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 Message Box service is used to display message boxes from the model of an MVVM-light app. The kit provides both the IMessageBoxService interface and its implementation MessageBoxService is 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 displaying a message box is shown below.

Cimbalino sample

Building the example code

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

Registering the service

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

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

Then we should implement the MainViewModel as in the following:
  1. using System.Collections.Generic;  
  2. using System.Windows;  
  3. using System.Windows.Input;  
  4.   
  5. using Cimbalino.Phone.Toolkit.Services;  
  6.   
  7. using GalaSoft.MvvmLight.Command;  
  8.   
  9. /// This class contains properties that the main View can data bind to.  
  10. public class MainViewModel : ViewModelBase  
  11. {  
  12.     /// The message box service.  
  13.     private readonly IMessageBoxService _messageBoxService;  
  14.   
  15.     /// Initializes a new instance of the MainViewModel class.  
  16.     public MainViewModel(IMessageBoxService messageBoxService)  
  17.     {  
  18.         _messageBoxService = messageBoxService;  
  19.         ShowMessageBoxCommand = new RelayCommand(ShowMessageBox);  
  20.     }  
  21.   
  22.     /// Shows the message box.  
  23.     private void ShowMessageBox()  
  24.     {  
  25.         _messageBoxService.Show("I am a message box""Cimbalino sample", MessageBoxButton.OK);  
  26.     }  
  27.   
  28.     /// Gets the show message box command.  
  29.     public ICommand ShowMessageBoxCommand { getprivate set; }  
  30. }  
Implementing the View

Add the binding in the main page is like:
  1. DataContext="{Binding MainViewModel,   
  2.                         Source={StaticResource Locator}}"  
  3. The MainPage.xaml can be the following:  
  4. <phone:PhoneApplicationPage x:Class="CimbalinoSample.MainPage"  
  5.                             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  6.                             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  7.                             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  8.                             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  9.                             xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"  
  10.                             xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"  
  11.                             FontFamily="{StaticResource PhoneFontFamilyNormal}"  
  12.                             FontSize="{StaticResource PhoneFontSizeNormal}"  
  13.                             Foreground="{StaticResource PhoneForegroundBrush}"  
  14.                             Orientation="Portrait"  
  15.                             DataContext="{Binding MainViewModel,   
  16.                                                   Source={StaticResource Locator}}"  
  17.                             SupportedOrientations="Portrait"  
  18.                             shell:SystemTray.IsVisible="True"  
  19.                             mc:Ignorable="d">  
  20.    
  21.     <!--  LayoutRoot is the root grid where all page content is placed  -->  
  22.     <Grid x:Name="LayoutRoot" Background="Transparent">  
  23.         <Grid.RowDefinitions>  
  24.             <RowDefinition Height="Auto" />  
  25.             <RowDefinition Height="*" />  
  26.         </Grid.RowDefinitions>  
  27.    
  28.         <!--  TitlePanel contains the name of the application and page title  -->  
  29.         <StackPanel x:Name="TitlePanel"  
  30.                     Grid.Row="0"  
  31.                     Margin="12,17,0,28">  
  32.             <TextBlock Margin="12,0"  
  33.                        Style="{StaticResource PhoneTextTitle2Style}"  
  34.                        Text="Cimbalino Sample" />  
  35.             <TextBlock Margin="9,-7,0,0"  
  36.                        Style="{StaticResource PhoneTextTitle2Style}"  
  37.                        Text="MessageBoxService" />  
  38.         </StackPanel>  
  39.    
  40.         <!--  ContentPanel - place additional content here  -->  
  41.         <Grid x:Name="ContentPanel"  
  42.               Grid.Row="1"  
  43.               Margin="12,0,12,0">  
  44.             <Button Command="{Binding ShowMessageBoxCommand}" Content="Show message box" />  
  45.         </Grid>  
  46.    
  47.     </Grid>  
  48.    
  49. </phone:PhoneApplicationPage>  

 


Similar Articles