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.
- <StackPanel>
- <TextBlock Text="Contact Card" Margin="10" FontSize="20"></TextBlock>
- <Grid Margin="20,10,0,0">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto"></RowDefinition>
- <RowDefinition Height="Auto"></RowDefinition>
- <RowDefinition Height="Auto"></RowDefinition>
- <RowDefinition Height="Auto"></RowDefinition>
- </Grid.RowDefinitions>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="Auto"></ColumnDefinition>
- <ColumnDefinition Width="*"></ColumnDefinition>
- </Grid.ColumnDefinitions>
-
- <TextBlock Text="Phone Number" VerticalAlignment="Center" ></TextBlock>
- <TextBox Name="phoneNumber" FontSize="20" Width="200" HorizontalAlignment="Left" Grid.Column="1"></TextBox>
- <TextBlock Text="E mail" VerticalAlignment="Center" Grid.Row="1"></TextBlock>
- <TextBox Name="mail" Grid.Column="1" FontSize="20" Width="200" HorizontalAlignment="Left" Grid.Row="1"></TextBox>
- <Button Name="getContact" Height="40" Width="140" Content="Get Contact" Click="getContact_Click" Grid.Row="2"></Button>
- <TextBlock Name="error" Foreground="Red" Grid.Row="3" FontSize="18" Grid.ColumnSpan="2" ></TextBlock>
- </Grid>
- </StackPanel>
Step 2: Add the following namespaces to your project which is needed in further C# code.
- using Windows.ApplicationModel.Contacts;
- 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,
- private void getContact_Click(object sender, RoutedEventArgs e)
- {
- Contact contact = CreateContactFromUserInput(mail, phoneNumber);
- if (contact != null)
- {
- // Show the contact card next to the button.
- Rect rect = MainPage.GetElementRect(sender as FrameworkElement);
-
- // Show with default placement.
- ContactManager.ShowContactCard(contact, rect);
- }
- }
- public Contact CreateContactFromUserInput(TextBox mail, TextBox phoneNumber)
- {
- if (mail.Text.Length == 0 && phoneNumber.Text.Length == 0)
- {
- error.Text = "You must enter an email address and/or phone number.";
- return null;
- }
-
- Contact contact = new Contact();
- if (mail.Text.Length > 0)
- {
- ContactEmail email = new ContactEmail() { Address = mail.Text };
- contact.Emails.Add(email);
- }
- if (phoneNumber.Text.Length > 0)
- {
- ContactPhone phone = new ContactPhone() { Number = phoneNumber.Text };
- contact.Phones.Add(phone);
- }
- return contact;
- }
- public static Rect GetElementRect(FrameworkElement element)
- {
- Windows.UI.Xaml.Media.GeneralTransform buttonTransform = element.TransformToVisual(null);
- Point point = buttonTransform.TransformPoint(new Point());
- return new Rect(point, new Size(element.ActualWidth, element.ActualHeight));
- }
Step 4: Run your application and test yourself.