The save phone number task is used to launch the built-in Contacts app, allowing the user to save a specified phone number. This chooser does not return data, but you can handle the Completed
event to determine whether the task was completed correctly.
SavePhoneNumberTask
contains a single PhoneNumber
property, which is passed to the Contacts application when the task’s Show
method is called.
The SavePhoneNumberTask
should be defined as a field in your class, like so:
readonly SavePhoneNumberTask savePhoneNumberTask
= new SavePhoneNumberTask();
Subscribe to the SavePhoneNumberTask.Completed
event within your class constructor, as shown:
savePhoneNumberTask.Completed
+= new EventHandler<TaskEventArgs>(HandleSavePhoneNumberTaskCompleted);
Use the Show
method of the SavePhoneNumberTask
to launch the built-in Contacts app. When the task completes the handler receives a TaskEventArgs
object, as shown:
void HandleSavePhoneNumberTaskCompleted(object sender, TaskEventArgs e)
{
if (e.Error != null)
{
Message = "Unable to save the phone number. " + e.Error;
return;
}
if (e.TaskResult == TaskResult.OK)
{
Message = "Phone number saved";
}
else if (e.TaskResult == TaskResult.Cancel)
{
Message = "Cancelled";
}
}
Example code for the SavePhoneNumberTask
can be found in the SavePhoneNumberViewModel
in the downloadable sample code.
The SavePhoneNumberView
page allows the user to enter a phone number into a TextBox
and then to initiate the savePhoneNumberTask
, using a button.
The button is bound to a viewmodel command named SaveCommand
.
The TextBox
is bound to the viewmodel’s PhoneNumber
property using a TwoWay
data binding, as shown:
<TextBox Text="{Binding PhoneNumber, Mode=TwoWay}"
InputScope="TelephoneNumber" />
The InputScope
property of the TextBox
causes the onscreen keyboard to be displayed with a set of keys suitable for typing a phone number. For more information on InputScopes
, see Chapter 6.
When the SaveCommand
is executed, the phoneNumber
text is assigned to the task, and the task’s Show
method is called. See the following excerpt:
public SavePhoneNumberViewModel() : base("Save Phone Number")
{
savePhoneNumberTask.Completed += savePhoneNumberTask_Completed;
saveCommand = new DelegateCommand(
delegate
{
Message = string.Empty;
savePhoneNumberTask.PhoneNumber = phoneNumber;
savePhoneNumberTask.Show();
});
}
Figure 14.17 shows the custom SavePhoneNumberView
page.
When the savePhoneNumberTask.Show
method is called, the Contacts application is launched (shown previously in Figure 14.8).
When the user selects either an existing contact, or the new contact option, the Edit Phone Number page is displayed (see Figure 14.18).
When the Save icon button is pressed, if the user selected an existing contact, the Edit Phone Contact page, shown previously in Figure 14.10, is displayed.
Alternatively, if the user opts to create a new contact, the New Contact page is displayed.
Once the contact information has been stored, the sample application is activated, and the Completed
handler is called.
18.216.151.164