×
Namespaces

Variants
Actions

Como enviar email usando Cimbalino Windows Phone Toolkit

From Nokia Developer Wiki
Jump to: navigation, search

Este artigo tem como objetivo mostrar como usar Cimbalino Windows Phone Toolkit - EmailComposeService.

WP Metro Icon Messages.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata

Exemplo de código
Código fonte: EmailComposeService (Github)

Testado com
SDK: Windows Phone 8.0 SDK, Windows Phone 7.1.1 SDK
Aparelho(s): Nokia Lumia 800, 920

Compatibilidade
Artigo
Tradução:
Por saramgsilva
Última alteração feita por saramgsilva em 23 Nov 2013

Contents

Introdução

Cimbalino Windows Phone Toolkit é um conjunto de itens uteis e poderosos para ajudar na implementação de aplicações Windows Phone. Projeto base do toolkit contendo serviços para implementação do padrão de MVVM, conversores, classes auxiliares, métodos de extensões;

Ecrã do examplo criado

SendMail.png

Contruindo o exemplo

Os pacotes estão disponíveis em Nuget Package Manager (para ambos os "targets") e podem ser instalados, para mais detalhes consultar: Como instalar os pacotes Cimbalino Windows Phone Toolkit. O exemplo deste artigo usa MVVM Light para ajudar na implementação do padrão MVVM, para mais detalhes consultar:

Registando os serviços

Devemos começar por registar cada serviço no ViewModelLocator, como podemos ver de seguida:

    /// This class contains static references to all the view models in the
/// application and provides an entry point for the bindings.
public class ViewModelLocator
{
/// Initializes a new instance of the ViewModelLocator class.
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()
{
// TODO Clear the ViewModels
}
}

Implementando a ViewModel

Em seguinda devemos implementar o MainViewModel, como podemos ver de seguida:

    /// This class contains properties that the main View can data bind to.
public class MainViewModel : ViewModelBase
{
/// The public application url.
private readonly string _appUrl;
 
/// The email compose service.
private readonly IEmailComposeService _emailComposeService;
 
/// The message
private string _message;
 
/// The subject
private string _subject;
 
/// The to
private string _to;
 
/// Initializes a new instance of the email Compose Service.
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");
}
 
/// Gets or sets the message.
public string Message
{
get { return _message; }
set { Set("Message", ref _message, value); }
}
 
/// Gets the send command.
public ICommand SendCommand { get; private set; }
 
/// Gets the send feedback command.
public ICommand SendFeedbackCommand { get; private set; }
 
/// Gets the share to e-mail command.
public ICommand ShareToMailCommand { get; private set; }
 
/// Gets or sets the subject.
public string Subject
{
get { return _subject; }
set { Set("Subject", ref _subject, value); }
}
 
 
/// Gets or sets recipient.
public string To
{
get { return _to; }
set { Set("To", ref _to, value); }
}
 
/// Sends mails.
private void Send()
{
if (!string.IsNullOrEmpty(To) && !string.IsNullOrEmpty(Subject) && !string.IsNullOrEmpty(Message))
{
_emailComposeService.Show(To, Subject, Message);
}
}
 
/// The send feedback.
private void SendFeedback()
{
const string to = "saramgsilva@gmail.com";
const string subject = "My Feedback";
var body = "This the body";
_emailComposeService.Show(to, subject, body);
}
 
/// The share to mail.
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);
}
}


Implementando a View

A MainPage.xaml será algo do género:

  1. <phone:PhoneApplicationPage x:Class="CimbalinoSample.MainPage"
  2.                             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.                             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.                             xmlns:cimbalino="clr-namespace:Cimbalino.Phone.Toolkit.Behaviors;assembly=Cimbalino.Phone.Toolkit"
  5.                             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6.                             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
  7.                             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8.                             xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
  9.                             xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
  10.                             DataContext="{Binding MainViewModel,
  11.                                                   Source={StaticResource Locator}}"
  12.                             FontFamily="{StaticResource PhoneFontFamilyNormal}"
  13.                             FontSize="{StaticResource PhoneFontSizeNormal}"
  14.                             Foreground="{StaticResource PhoneForegroundBrush}"
  15.                             Orientation="Portrait"
  16.                             SupportedOrientations="Portrait"
  17.                             shell:SystemTray.IsVisible="True"
  18.                             mc:Ignorable="d">
  19.  
  20.     <!--  LayoutRoot is the root grid where all page content is placed  -->
  21.     <Grid x:Name="LayoutRoot" Background="Transparent">
  22.         <Grid.RowDefinitions>
  23.             <RowDefinition Height="Auto" />
  24.             <RowDefinition Height="*" />
  25.         </Grid.RowDefinitions>
  26.  
  27.         <!--  TitlePanel contains the name of the application and page title  -->
  28.         <StackPanel x:Name="TitlePanel"
  29.                     Grid.Row="1"
  30.                     Margin="0,5,12,396">
  31.             <TextBlock Margin="12,0"
  32.                        Style="{StaticResource PhoneTextTitle2Style}"
  33.                        Text="Cimbalino Toolkit Sample" />
  34.         </StackPanel>
  35.         <TextBlock Grid.RowSpan="2"
  36.                    Margin="12,50,-3,639"
  37.                    Style="{StaticResource PhoneTextTitle3Style}"
  38.                    TextWrapping="Wrap">
  39.             This samples has the goal to show how to use Cimbalino Toolkit - EmailComposeService.
  40.         </TextBlock>
  41.         <!--  ContentPanel - place additional content here  -->
  42.         <Grid x:Name="ContentPanel"
  43.               Grid.Row="1"
  44.               Margin="12,150,12,0">
  45.             <Grid.RowDefinitions>
  46.                 <RowDefinition Height="Auto" />
  47.                 <RowDefinition Height="Auto" />
  48.                 <RowDefinition Height="Auto" />
  49.                 <RowDefinition Height="Auto" />
  50.                 <RowDefinition Height="Auto" />
  51.                 <RowDefinition Height="Auto" />
  52.                 <RowDefinition Height="Auto" />
  53.                 <RowDefinition Height="Auto" />
  54.             </Grid.RowDefinitions>
  55.             <TextBlock Grid.Row="0" Text="To:" />
  56.             <TextBox Grid.Row="1" Text="{Binding To, Mode=TwoWay}" />
  57.  
  58.             <TextBlock Grid.Row="2" Text="Subject:" />
  59.             <TextBox Grid.Row="3" Text="{Binding Subject, Mode=TwoWay}" />
  60.             <TextBlock Grid.Row="4" Text="Message:" />
  61.             <TextBox Grid.Row="5"
  62.                      Height="200"
  63.                      Text="{Binding Message,
  64.                                     Mode=TwoWay}" />
  65.             <Button Grid.Row="6"
  66.                     Command="{Binding SendCommand}"
  67.                     Content="Send" />
  68.         </Grid>
  69.         <i:Interaction.Behaviors>
  70.             <cimbalino:ApplicationBarBehavior>
  71.  
  72.                 <cimbalino:ApplicationBarIconButton Command="{Binding SendFeedbackCommand,
  73.                                                                       Mode=OneTime}"
  74.                                                     IconUri="/Images/appbar.reply.email.png"
  75.                                                     Text="Feedback" />
  76.                 <cimbalino:ApplicationBarIconButton Command="{Binding ShareToMailCommand,
  77.                                                                       Mode=OneTime}"
  78.                                                     IconUri="/Images/appbar.email.png"
  79.                                                     Text="Email" />
  80.             </cimbalino:ApplicationBarBehavior>
  81.         </i:Interaction.Behaviors>
  82.     </Grid>
  83.  
  84. </phone:PhoneApplicationPage>

Exemplo Relacionado


Código Fonte

O código fonte pode ser obtido aqui.

This page was last modified on 23 November 2013, at 22:09.
96 page views in the last 30 days.