The SaveEmailAddressTask
is a chooser that allows the user to save a specified email address using the built-in Contacts application. An email address can be saved to an existing contact or to a new contact. SaveEmailAddressTask
does not return data, but its Completed
event can be handled to determine whether the task was completed correctly.
The SaveEmailAddressTask
should be defined as a field in your class, like so:
readonly SaveEmailAddressTask saveEmailAddressTask
= new SaveEmailAddressTask();
Subscribe to the SaveEmailAddressTask.Completed
event within your class constructor, as shown:
saveEmailAddressTask.Completed += HandleSaveEmailAddressTaskCompleted;
Use the Show
method of the SaveEmailAddressTask
to launch the built-in Contacts app. When the task completes the handler receives a TaskEventArgs
object, as shown:
void HandleSaveEmailAddressTaskCompleted(object sender, TaskEventArgs e)
{
if (e.Error != null)
{
Message = "Unable to save the email address. " + e.Error;
return;
}
if (e.TaskResult == TaskResult.OK)
{
Message = "Email address saved";
}
else if (e.TaskResult == TaskResult.Cancel)
{
Message = "Cancelled";
}
}
Example code for the SaveEmailAddressTask
can be found in the SaveEmailAddressViewModel
in the downloadable sample code.
The SaveEmailAddressView
page allows the user to enter an email address and then to initiate the SaveEmailAddressTask
using a button (see Figure 14.7).
The view’s Email Address TextBlock
has a two-way binding to the viewmodel’s EmailAddress
property, as shown:
<TextBox Text="{Binding EmailAddress, Mode=TwoWay}"
InputScope="EmailSmtpAddress" />
The InputScope
property of the TextBox
causes the onscreen keyboard to be displayed with a set of keys suitable for typing an email address. For more information on InputScopes
, see Chapter 6, “Mastering Text Elements and Fonts.”
To initiate the SaveEmailAddressTask
, the viewmodel contains a public property called SaveCommand
:
readonly DelegateCommand saveCommand;
public ICommand SaveCommand
{
get
{
return saveCommand;
}
}
When the saveCommand
is executed, the Show
method of the saveEmailAddressTask
is called. This behavior is defined in the viewmodel’s construct, as shown in the following excerpt:
public SaveEmailAddressViewModel() : base("Save Email Address")
{
saveEmailAddressTask.Completed += saveEmailAddressTask_Completed;
saveCommand = new DelegateCommand(
delegate
{
Message = string.Empty;
saveEmailAddressTask.Email = emailAddress;
saveEmailAddressTask.Show();
});
}
The view’s Save button is bound to the SaveCommand
, as shown:
<Button Command="{Binding SaveCommand}"
Content="Save" />
When the saveEmailAddressTask.Show
method is called, the built-in Contacts application is launched (see Figure 14.8).
When the user selects either an existing contact or the new contact option, the Edit Email page is displayed (see Figure 14.9).
If the user selects an existing contact, when the Save icon button is tapped, the Edit Phone Contact page is displayed (see Figure 14.10).
Alternatively, if the user opts to create a new contact, the New Contact page is displayed.
After the contact information is saved using the disk icon, the sample application is activated, and the HandleSaveEmailAddressTaskCompleted
handler is called.
18.117.183.252