In this article we are going to see how to programmatically access the contact database and search for a contact photo to be used in a Windows Phone application development. So to access the contact information, we need to create an instance of the Contact object and get the information that are required to be shown in the application. Since we need to access the photo of the contact, we will create a new Bitmap image and set it source to get the picture from the contact object. Let us see the steps on achieving this task, Open Visual Studio 2012 IDE and create a new Windows Phone project with a valid project name as shown in the screen below. [more]
Clicking on OK will create the project and the solution with the list of default files and folders that are required to run the application. It will take some time to create these files based on your system configuration, so once everything is ready we can see the Visual Studio IDE with the project as shown in the screen below.
Now let us add some control which basically triggers an event to call the Contact class and get the photo of a contact which we require to be shown in our application. But basically since we are going to run the application locally in the emulator we will not have any photo assigned for any contacts, so to check the application effectively run the application in the device.
XAML Code:
[code:c#]<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="Photo" 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="Get Contact Image" HorizontalAlignment="Left" Margin="51,29,0,0" VerticalAlignment="Top" Width="353" Click="Button_Click"/>
<Image HorizontalAlignment="Left" Height="307" Margin="69,123,0,0" VerticalAlignment="Top" Width="319"/>
</Grid>
[/code]Now in the code behind add the below code which basically creates an instance of the Contact class and do a Asynchronous contact search. Basically we will try to get the first contact from the search and set its source to get the picture that we need to show it in the application as shown in the code below.
C# Code:
[code:c#]using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using F5debugHowto60.Resources;
using Microsoft.Phone.UserData;
using System.Windows.Media.Imaging;
namespace F5debugHowto60
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Contacts cCont = new Contacts();
cCont.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchDone);
cCont.SearchAsync(String.Empty, FilterKind.DisplayName, "F5debug");
}
void Contacts_SearchDone(object sender, ContactsSearchEventArgs e)
{
try
{
Contact conts = e.Results.First();
BitmapImage img = new BitmapImage();
img.SetSource(conts.GetPicture());
imgPicture.Source = img;
}
catch (Exception)
{
}
}
}
}
Now we are done with our configuration, 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.
No Comments
Como puedo mejorar la velocidad de internet de mi samsung galaxy s2…esta demasiado lento con paquete de datos.