How to Use Cimbalino Windows Phone Toolkit Camera - CameraCaptureService

This article shows how to launch the Camera "chooser" using the CameraCaptureService from the Cimbalino Windows Phone Toolkit. 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.

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. Cimbalino.Phone.Toolkit.Camera provides the services for camera access.

The Cimbalino Toolkit's "Camera Capture" service provides an MVVM compatible service for launching the Camera application and returning the captured photo. The kit provides both the ICameraCaptureService interface and its implementation CameraCaptureService required to register the service in MVVM Light (note that MVVM and the MVVM Light Toolkit are not "preconditions" to use this service).

Building the example code

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

Note

You must specify the following capabilities in the app manifest: ID_CAP_ISV_CAMERA.

Registering the service

Register the service in the ViewModelLocator constructor as in the 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<ICameraCaptureService>())  
  11.         {  
  12.             SimpleIoc.Default.Register<ICameraCaptureService, CameraCaptureService>();  
  13.         }  
  14.   
  15.         SimpleIoc.Default.Register<MainViewModel>();  
  16.     }  
  17.   
  18.     public MainViewModel MainViewModel  
  19.     {  
  20.         get  
  21.         {  
  22.             return ServiceLocator.Current.GetInstance<MainViewModel>();  
  23.         }  
  24.     }  
  25.   
  26.     public static void Cleanup()  
  27.     {  
  28.         // TODO Clear the ViewModels  
  29.     }  
  30. }  
Implementing the ViewModel

Implement the MainViewModel as shown below. The highlighted sections show the MainViewModel constructor taking the ICameraCaptureService parameter and assigning it to the private variable and later on the variable being used to launch the camera.
  1. /// This class contains properties that the main View can data bind to.  
  2. public class MainViewModel : ViewModelBase  
  3. {  
  4.     /// The camera capture service  
  5.     private readonly ICameraCaptureService _cameraCaptureService;  
  6.   
  7.     /// The image  
  8.     private ImageSource _image;  
  9.   
  10.     /// Initializes a new instance of the MainViewModel class.  
  11.     public MainViewModel(ICameraCaptureService cameraCaptureService)  
  12.     {  
  13.         _cameraCaptureService = cameraCaptureService;  
  14.         CameraCaptureCommand = new RelayCommand(CameraCapture);  
  15.     }  
  16.   
  17.     /// Gets the camera capture command.  
  18.     public ICommand CameraCaptureCommand { getprivate set; }  
  19.   
  20.     /// Gets or sets the image.  
  21.     public ImageSource Image  
  22.     {  
  23.         get  
  24.         {  
  25.             return _image;  
  26.         }  
  27.         set  
  28.         {  
  29.             Set("Image"ref _image, value);  
  30.         }  
  31.     }  
  32.   
  33.   
  34.     /// Cameras the capture.  
  35.     private void CameraCapture()  
  36.     {  
  37.         Image = null;  
  38.         _cameraCaptureService.Show(CameraCapetureResult);  
  39.     }  
  40.   
  41.   
  42.     /// Cameras capture result.  
  43.     /// "photoResult" is he photo result.  
  44.     private async void CameraCapetureResult(Microsoft.Phone.Tasks.PhotoResult photoResult)  
  45.     {  
  46.         if(photoResult.ChosenPhoto!=null)  
  47.         {  
  48.             var bitmapImage = new BitmapImage();  
  49.             bitmapImage.SetSource(photoResult.ChosenPhoto);  
  50.             Image = bitmapImage;  
  51.         }  
  52.     }  
  53. }  
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.
This XAML statement binds the view to the viewmodel.
  1. DataContext="{Binding MainViewModel,   
  2. Source={StaticResource Locator}}"  
The MainPage.xaml can be the following:
  1. <phone:PhoneApplicationPage  
  2.     x:Class="CimbalinoSample.MainPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"  
  6.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"  
  7.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  8.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  9.     mc:Ignorable="d"  
  10.     FontFamily="{StaticResource PhoneFontFamilyNormal}"  
  11.     FontSize="{StaticResource PhoneFontSizeNormal}"  
  12.     Foreground="{StaticResource PhoneForegroundBrush}"  
  13.     DataContext="{Binding MainViewModel, Source={StaticResource Locator}}"  
  14.     SupportedOrientations="Portrait" Orientation="Portrait"  
  15.     shell:SystemTray.IsVisible="True">  
  16.    
  17.     <!--LayoutRoot is the root grid where all page content is placed-->  
  18.     <Grid x:Name="LayoutRoot" Background="Transparent">  
  19.         <Grid.RowDefinitions>  
  20.             <RowDefinition Height="Auto"/>  
  21.             <RowDefinition Height="*"/>  
  22.         </Grid.RowDefinitions>  
  23.    
  24.         <!--TitlePanel contains the name of the application and page title-->  
  25.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">  
  26.             <TextBlock Text="Cimbalino Sample" Style="{StaticResource PhoneTextTitle2Style}" Margin="12,0"/>  
  27.             <TextBlock Text="CameraCaptureService and PhotoCameraService"   
  28.                        TextWrapping="Wrap"  
  29.                        Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle2Style}"/>  
  30.         </StackPanel>  
  31.    
  32.         <!--ContentPanel - place additional content here-->  
  33.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">  
  34.             <Image Source="{Binding Image}" Stretch="Uniform" Margin="0,242,0,0" />  
  35.             <Button Content="Camera Capture" Command="{Binding CameraCaptureCommand}" Height="237" VerticalAlignment="Top"/>  
  36.    
  37.         </Grid>  
  38.     </Grid>  
  39. </phone:PhoneApplicationPage>