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 different 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 Cimbalino Toolkit's "Email Compose" service is an MVVM compatible wrapper around the system EmailComposeTask that can be used to launch a dialog to email users. The kit provides both the IEmailComposeService interface and its implementation EmailComposeService required to register the service in MVVM Light (note that MVVM and the MVVM Light Toolkit are not "preconditions" to use this service).
Screenshot of the example app:
Building the Example Code
The source code for the code example is available here: EmailComposeService (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:
-
-
- public class ViewModelLocator
- {
-
- public ViewModelLocator()
- {
- ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
- if (!SimpleIoc.Default.IsRegistered<IEmailComposeService>())
- {
- SimpleIoc.Default.Register<IEmailComposeService, EmailComposeService>();
- }
- SimpleIoc.Default.Register<MainViewModel>();
- }
-
- public MainViewModel MainViewModel
- {
- get
- {
- return ServiceLocator.Current.GetInstance<MainViewModel>();
- }
- }
-
- public static void Cleanup()
- {
-
- }
- }
Implementing the ViewModel
Implement the MainViewModel as shown below. The highlighted sections show the MainViewModel constructor taking the IEmailComposeService parameter and assigning it to the private variable. Later on the private member is used to show the link service.
-
- public class MainViewModel : ViewModelBase
- {
-
- private readonly string _appUrl;
-
- private readonly IEmailComposeService _emailComposeService;
-
- private string _message;
-
- private string _subject;
-
- private string _to;
-
-
- public MainViewModel(IEmailComposeService emailComposeService)
- {
- _emailComposeService = emailComposeService;
- SendFeedbackCommand = new RelayCommand(SendFeedback);
- ShareToMailCommand = new RelayCommand(ShareToMail);
- SendCommand =new RelayCommand(Send);
- _appUrl = string.Concat("http://windowsphone.com/s?appid=8df00038-1b7a-406b-b33f-37a78b17348c ");
- }
-
-
- public string Message
- {
- get { return _message; }
- set { Set("Message", ref _message, value); }
- }
-
- public ICommand SendCommand { get; private set; }
-
- public ICommand SendFeedbackCommand { get; private set; }
-
- public ICommand ShareToMailCommand { get; private set; }
-
- public string Subject
- {
- get { return _subject; }
- set { Set("Subject", ref _subject, value); }
- }
-
- public string To
- {
- get { return _to; }
- set { Set("To", ref _to, value); }
- }
-
- private void Send()
- {
- if (!string.IsNullOrEmpty(To) && !string.IsNullOrEmpty(Subject) && !string.IsNullOrEmpty(Message))
- {
- _emailComposeService.Show(To, Subject, Message);
- }
- }
-
- private void SendFeedback()
- {
- const string to = "[email protected]";
- const string subject = "My Feedback";
- var body = "This the body";
- _emailComposeService.Show(to, subject, body);
- }
-
- private void ShareToMail()
- {
- const string Subject = "Cimbalino Toolkit Sample";
- var body = string.Concat("This application is amazing, you should try it! See in", _appUrl);
- _emailComposeService.Show(Subject, body);
- }
- }
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.
Add the binding to the main page as shown:
- DataContext="{Binding MainViewModel, Source={StaticResource Locator}}"
The MainPage.xaml can be the following:
- <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,639" Style="{StaticResource PhoneTextTitle3Style}" TextWrapping="Wrap">
- This samples has the goal to show how to use Cimbalino Toolkit - EmailComposeService.
- </TextBlock>
-
- <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,150,12,0">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto" />
- <RowDefinition Height="Auto" />
- <RowDefinition Height="Auto" />
- <RowDefinition Height="Auto" />
- <RowDefinition Height="Auto" />
- <RowDefinition Height="Auto" />
- <RowDefinition Height="Auto" />
- <RowDefinition Height="Auto" />
- </Grid.RowDefinitions>
- <TextBlock Grid.Row="0" Text="To:" />
- <TextBox Grid.Row="1" Text="{Binding To, Mode=TwoWay}" />
- <TextBlock Grid.Row="2" Text="Subject:" />
- <TextBox Grid.Row="3" Text="{Binding Subject, Mode=TwoWay}" />
- <TextBlock Grid.Row="4" Text="Message:" />
- <TextBox Grid.Row="5" Height="200" Text="{Binding Message, Mode=TwoWay}" />
- <Button Grid.Row="6" Command="{Binding SendCommand}" Content="Send" />
- </Grid>
- <i:Interaction.Behaviors>
- <cimbalino:ApplicationBarBehavior>
- <cimbalino:ApplicationBarIconButton Command="{Binding SendFeedbackCommand, Mode=OneTime}" IconUri="/Images/appbar.reply.email.png" Text="Feedback" />
- <cimbalino:ApplicationBarIconButton Command="{Binding ShareToMailCommand, Mode=OneTime}" IconUri="/Images/appbar.email.png" Text="Email" />
- </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 the main page.
- MainPage.xaml and MainPage.xaml.cs represent the main page.
See Also