In this tutorial we are going to see how to make use of the Save Phone Number task in Windows Phone development. This task is something similar to the Save Email Address task which we have seen in our earlier tutorial, this task is also common that it will be used in our day to day work. With this task we can save the Phone number to a contact on the user selection. With this task we can trigger to save the Phone number from our application with simple clicks. This task opens the contact application and provides an option to the end user to select the contact to which the phone number needs to be saved. Let us see the steps on how to achieve this task in real time for a Windows phone application. [more]
Open Visual Studio 2010 IDE and create a new Silverlight for Windows Phone project with a valid project name as shown in the screen below. Once the project is created add some controls which are used to trigger the Save Phone Number Task as shown in the screen below.
Now drag and drop few controls to the screen which is used to trigger event that launches the Save Phone Number Task. Once we designed the screen with the controls we can see the screen looks like below. We have added a button control to trigger the event and text block control to show the end result after the task is completed as shown in the screen below.
XAML Code:
<phone:PhoneApplicationPage
x:Class="F5debugHowto26.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!–LayoutRoot is the root grid where all page content is placed–>
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!–TitlePanel contains the name of the application and page title–>
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="F5debug How to Series" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="Save Phone Number Task" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!–ContentPanel – place additional content here–>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Content="Save Phone Number" Height="72" HorizontalAlignment="Left" Margin="9,6,0,0" Name="button1" VerticalAlignment="Top" Width="441" Click="button1_Click" />
<TextBlock Height="277" HorizontalAlignment="Left" TextWrapping="Wrap" Margin="29,108,0,0" Name="textBlock1" Text="" VerticalAlignment="Top" Width="407" />
</Grid>
</Grid>
</phone:PhoneApplicationPage>
Now we need to write our code in the button click event to trigger the Save Phone Number task, which basically opens the Contacts application to select a contact to which the Phone number need to be added. To do that just go to the code behind and first add the USING handler code on top with the existing using statements as shown in the code below.
using Microsoft.Phone.Tasks;
Next is to add the below code to the button click event which basically creates an instance of the Save Phone Number task and once the event is completed another event is raised which takes the result as output with which we can conclude on the action taken as shown in the code below.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
namespace F5debugHowto26
{
public partial class MainPage : PhoneApplicationPage
{
SavePhoneNumberTask spnTask;
// Constructor
public MainPage()
{
InitializeComponent();
spnTask = new SavePhoneNumberTask();
spnTask.Completed += new EventHandler<TaskEventArgs>(spnTask_Completed);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
spnTask.PhoneNumber = "0123456789";
spnTask.Show();
}
void spnTask_Completed(object sender, TaskEventArgs e)
{
if (e.TaskResult == TaskResult.OK)
{
textBlock1.Text = "Phone Number Saved Successfully!!!";
}
else if (e.TaskResult == TaskResult.Cancel)
{
textBlock1.Text = "User Cancellation!!!";
}
else
{
textBlock1.Text = "Phone Number couldnot be saved!!!";
}
}
}
}
Now we are done with our code, just run the application by pressing F5 directly from the keyboard or we can use the Build and execute the project option from the tool bar to run the application. Once the Build is successful we can see the Windows Phone emulator with the application and the expected outputs as shown in the screens below.
Output Screens:
So in this tutorial we have seen how to use the Save Phone Number task to save the Phone number of a specified contact by launching the contact application directly from the application which is very much useful in our day to day tasks. That’s it from this tutorial on Windows Phone see you all in the next tutorial soon. Mean while Happy Programming!!!
No Comments
It's fantastic that you are getting thoughts from this piece of writing as well as from our discussion made at this time.|
I used to be recommended this web site via my cousin. I'm no longer positive whether this put up is written through him as nobody else recognize such designated about my trouble. You are incredible! Thank you!|
This information is priceless. Where can I find out more?|