Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Showing Contact Details From Windows Phone 8.1 using C#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 655
    Comment on it

    Open Visual Studio and select Windows Phone from Visual C# templates. From the available project types, select "Blank App(Windows Phone)" and click OK.

     

    Creating a Model

    Right click on project and add a new class file PhoneContact.cs. It contains the properties to hold the phone contact details, we are interested in.
     

    PhoneContact.cs

    using Windows.UI.Xaml.Media.Imaging;
    
    public class PhoneContact
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public BitmapImage ContactImage { get; set; }
    }


    Adding XAML in MainPage.xaml to show list of contacts
     

    Open MainPage.xaml file and replace the empty Grid inside the Page tag with following XAML.

    <ListBox x:Name="lbxContacts">
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            </Style>
        </ListBox.ItemContainerStyle>
    
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Background="#0057A3" Height="80">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
    
                    <Image Grid.Column="0" Margin="0,4,0,4" Source="{Binding ContactImage}"/>
    
                    <StackPanel Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Left">
                        <TextBlock VerticalAlignment="Center" Text="{Binding FirstName}" Foreground="White" FontSize="20" Padding="0,0,6,0"/>
                        <TextBlock VerticalAlignment="Center" Text="{Binding LastName}" Foreground="White" FontSize="20"/>
                    </StackPanel>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

     

    Adding code in MainPage.xaml.cs to fetch contact details from phone


    Open MainPage.xaml.cs and add the following namespaces and code to get the contacts from the phone.

    using System;
    using System.Collections.Generic;
    using Windows.ApplicationModel.Contacts;
    using Windows.Storage;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Media.Imaging;
    using Windows.UI.Xaml.Navigation;
    
    
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        // List to hold phone contacts
        private List<PhoneContact> phoneContacts = new List<PhoneContact>();
    
        public MainPage()
        {
            this.InitializeComponent();
    
            this.NavigationCacheMode = NavigationCacheMode.Required;
        }
    
        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // TODO: Prepare page for display here.
    
            // TODO: If your application contains multiple pages, ensure that you are
            // handling the hardware Back button by registering for the
            // Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
            // If you are using the NavigationHelper provided by some templates,
            // this event is handled for you.
    
            GetPhoneContacts();
        }
    
        private async void GetPhoneContacts()
        {
            ContactStore contactStore = await ContactManager.RequestStoreAsync();
            IReadOnlyList<Contact> contacts = null;
    
            // Get all contacts
            contacts = await contactStore.FindContactsAsync();
    
            // Loop through each contact
            foreach (var item in contacts)
            {
                if (!string.IsNullOrEmpty(item.FirstName))
                {
                    BitmapImage bmp = null;
    
                    // Get the contact image
                    if (item.Thumbnail != null)
                        bmp = new BitmapImage(new Uri(((StorageFile)item.Thumbnail).Path));
    
                    var phoneContact = new PhoneContact()
                    {
                        FirstName = item.FirstName,
                        LastName = item.LastName,
                        ContactImage = bmp
                    };
    
                    // Add the contact detail to list
                    phoneContacts.Add(phoneContact);
                }
            }
    
            // Setting the contact list as the datasource for ListBox lbxContacts
            lbxContacts.ItemsSource = phoneContacts;
        }
    }

     

    I have tested the above code in Emulator after adding some contact details and the final output is as shown in the following image.
     


    Hope you have enjoyed this article. :)

    Showing Contact Details From Windows Phone 8.1 using C#

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: