Contact Card In Windows 10

Here are the steps:
 
Step 1: Open a blank app and add two Textboxes and a button either from the toolbox or by copying the following XAML code into your grid. Here we have used an additional grid to hold our tools. 
  1. <StackPanel>  
  2. <TextBlock Text="Contact Card" Margin="10" FontSize="20"></TextBlock>  
  3. <Grid Margin="20,10,0,0">  
  4. <Grid.RowDefinitions>  
  5. <RowDefinition Height="Auto"></RowDefinition>  
  6. <RowDefinition Height="Auto"></RowDefinition>  
  7. <RowDefinition Height="Auto"></RowDefinition>  
  8. <RowDefinition Height="Auto"></RowDefinition>  
  9. </Grid.RowDefinitions>  
  10. <Grid.ColumnDefinitions>  
  11. <ColumnDefinition Width="Auto"></ColumnDefinition>  
  12. <ColumnDefinition Width="*"></ColumnDefinition>  
  13. </Grid.ColumnDefinitions>  
  14.   
  15. <TextBlock Text="Phone Number" VerticalAlignment="Center" ></TextBlock>  
  16. <TextBox Name="phoneNumber" FontSize="20" Width="200" HorizontalAlignment="Left" Grid.Column="1"></TextBox>  
  17. <TextBlock Text="E mail" VerticalAlignment="Center" Grid.Row="1"></TextBlock>  
  18. <TextBox Name="mail" Grid.Column="1" FontSize="20" Width="200" HorizontalAlignment="Left" Grid.Row="1"></TextBox>  
  19. <Button Name="getContact" Height="40" Width="140" Content="Get Contact" Click="getContact_Click" Grid.Row="2"></Button>  
  20. <TextBlock Name="error" Foreground="Red" Grid.Row="3" FontSize="18" Grid.ColumnSpan="2" ></TextBlock>  
  21. </Grid>  
  22. </StackPanel>  


Step 2:
Add the following namespaces to your project which is needed in further C# code.
  1. using Windows.ApplicationModel.Contacts;  
  2. using Windows.Foundation;  

Step 3: Copy and paste the following code to the cs page which will be called on button click event and will pop up the contact from the phone number or mail,

  1. private void getContact_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     Contact contact = CreateContactFromUserInput(mail, phoneNumber);  
  4.     if (contact != null)  
  5.     {  
  6.         // Show the contact card next to the button.  
  7.         Rect rect = MainPage.GetElementRect(sender as FrameworkElement);  
  8.   
  9.         // Show with default placement.  
  10.         ContactManager.ShowContactCard(contact, rect);  
  11.     }  
  12. }  
  13. public Contact CreateContactFromUserInput(TextBox mail, TextBox phoneNumber)  
  14. {  
  15.     if (mail.Text.Length == 0 && phoneNumber.Text.Length == 0)  
  16.     {  
  17.         error.Text = "You must enter an email address and/or phone number.";  
  18.         return null;  
  19.     }  
  20.   
  21.     Contact contact = new Contact();  
  22.     if (mail.Text.Length > 0)  
  23.     {  
  24.         ContactEmail email = new ContactEmail() { Address = mail.Text };  
  25.         contact.Emails.Add(email);  
  26.     }  
  27.     if (phoneNumber.Text.Length > 0)  
  28.     {  
  29.         ContactPhone phone = new ContactPhone() { Number = phoneNumber.Text };  
  30.         contact.Phones.Add(phone);  
  31.     }  
  32.     return contact;  
  33. }  
  34. public static Rect GetElementRect(FrameworkElement element)  
  35. {  
  36.     Windows.UI.Xaml.Media.GeneralTransform buttonTransform = element.TransformToVisual(null);  
  37.     Point point = buttonTransform.TransformPoint(new Point());  
  38.     return new Rect(point, new Size(element.ActualWidth, element.ActualHeight));  
  39. }  

Step 4: Run your application and test yourself. 

Up Next
    Ebook Download
    View all
    Learn
    View all