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. Cimbalino.Phone.Toolkit.MediaLibrary is a MVVM compatible service for media library access.
The Cimbalino Toolkit's "Screenshot service" takes a screenshot of the current screen and saves it to theMediaLibrary with either a specified name or a name based on a GUID. The kit provides both the IScreenshotService interface and its implementation ScreenshotService 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: ScreenshotService 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_MEDIALIB_PHOTO.
Registering the service
Register the service in the ViewModelLocator constructor as highlighted below:
- using Cimbalino.Phone.Toolkit.Services;
- using GalaSoft.MvvmLight;
- using GalaSoft.MvvmLight.Ioc;
- using Microsoft.Practices.ServiceLocation;
-
-
-
- public class ViewModelLocator
- {
-
- public ViewModelLocator()
- {
- ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
- if (!SimpleIoc.Default.IsRegistered<IScreenshotService>())
- {
- SimpleIoc.Default.Register<IScreenshotService, ScreenshotService>();
- }
- SimpleIoc.Default.Register<MainViewModel>();
- }
-
-
- public MainViewModel MainViewModel
- {
- get
- {
- return ServiceLocator.Current.GetInstance<MainViewModel>();
- }
- }
-
- public static void Cleanup()
- {
-
- var viewModelLocator = (ViewModelLocator)App.Current.Resources["Locator"];
- viewModelLocator.MainViewModel.Cleanup();
- }
- }
Implementing the ViewModel
Then we should implement the MainViewModel as in the following. Note how the model takes a IScreenshotService - the ViewModelLocatorautomatically creates a registered object of this type to satisfy the dependency.
- using System.Windows;
- using System.Windows.Input;
- using System.Windows.Threading;
-
- using Cimbalino.Phone.Toolkit.Services;
-
- using GalaSoft.MvvmLight.Command;
- using GalaSoft.MvvmLight;
-
-
- public class MainViewModel : ViewModelBase
- {
-
- private readonly IScreenshotService _screenshotService;
-
-
- public MainViewModel(IScreenshotService screenshotService)
- {
- _screenshotService = screenshotService;
- TakeScreenshotCommand = new RelayCommand(TakeScreenshot);
- }
-
-
- public ICommand TakeScreenshotCommand { get; private set; }
-
-
- private void TakeScreenshot()
- {
- _screenshotService.TakeScreenshot("CimbalinoScreenshot");
- }
- }
Implementing the View
The MainPage.xaml is as shown below:
- <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:d="http://schemas.microsoft.com/expression/blend/2008"
- 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="0"
- Margin="12,17,0,28">
- <TextBlock Margin="12,0"
- Style="{StaticResource PhoneTextTitle2Style}"
- Text="Cimbalino Sample" />
- <TextBlock Margin="9,-7,0,0"
- Style="{StaticResource PhoneTextTitle2Style}"
- Text="ScreenshotService" />
- </StackPanel>
-
-
- <Grid x:Name="ContentPanel"
- Grid.Row="1"
- Margin="12,0,12,0">
- <TextBlock TextWrapping="Wrap">This samples has the goal to show how to use Cimbalino Windows Phone Toolkit Media Library - ScreenshotService</TextBlock>
-
- <Button Margin="0,219,0,293"
- Command="{Binding TakeScreenshotCommand}"
- Content="Take Screenshot" />
- </Grid>
- </Grid>
- </phone:PhoneApplicationPage>