Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
How to create contactcards in UWP App
WhatsApp
Muthuramalingam Duraipandi
Aug 09
2016
1.4
k
0
0
contact.ZIP
//MainPage1:
using
System;
using
System.Collections.Generic;
using
Windows.ApplicationModel.Contacts;
using
Windows.Foundation;
using
Windows.UI.Xaml;
using
Windows.UI.Xaml.Controls;
namespace
SDKTemplate
{
public
partial
class
MainPage : Page
{
public
const
string
FEATURE_NAME =
"Contact cards C# sample"
;
List<Scenario> scenarios =
new
List<Scenario>
{
new
Scenario() { Title=
"Show mini contact card"
, ClassType=
typeof
(Scenario1_Mini)},
new
Scenario() { Title=
"Show mini contact card with delayed information"
, ClassType=
typeof
(Scenario2_DelayMini)},
new
Scenario() { Title=
"Show full contact card"
, ClassType=
typeof
(Scenario3_Full)},
};
public
Contact CreateContactFromUserInput(TextBox EmailAddress, TextBox PhoneNumber)
{
if
(EmailAddress.Text.Length == 0 && PhoneNumber.Text.Length == 0)
{
NotifyUser(
"You must enter an email address and/or phone number."
, NotifyType.ErrorMessage);
return
null
;
}
Contact contact =
new
Contact();
// Maximum length for email address is 321, enforced by XAML markup.
if
(EmailAddress.Text.Length > 0)
{
ContactEmail email =
new
ContactEmail() { Address = EmailAddress.Text };
contact.Emails.Add(email);
}
// Maximum length for phone number is 50, enforced by XAML markup.
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));
}
}
public
class
Scenario
{
public
string
Title {
get
;
set
; }
public
Type ClassType {
get
;
set
; }
}
}
//Scenario_Mini1
using
Windows.Foundation;
using
Windows.UI.Popups;
using
Windows.UI.Xaml;
using
Windows.UI.Xaml.Controls;
using
Windows.UI.Xaml.Navigation;
namespace
SDKTemplate
{
public
sealed
partial
class
Scenario1_Mini : Page
{
private
MainPage rootPage = MainPage.Current;
public
Scenario1_Mini()
{
this
.InitializeComponent();
}
protected
override
void
OnNavigatedTo(NavigationEventArgs e)
{
if
(!ContactManager.IsShowContactCardSupported())
{
NotSupportedWarning.Visibility = Visibility.Visible;
}
}
private
void
ShowContactCard_Click(
object
sender, RoutedEventArgs e)
{
Contact contact = rootPage.CreateContactFromUserInput(EmailAddress, 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);
}
}
private
void
ShowContactCardWithPlacement_Click(
object
sender, RoutedEventArgs e)
{
Contact contact = rootPage.CreateContactFromUserInput(EmailAddress, PhoneNumber);
if
(contact !=
null
)
{
// Show the contact card next to the button.
Rect rect = MainPage.GetElementRect(sender
as
FrameworkElement);
// Show with preferred placement to the right.
ContactManager.ShowContactCard(contact, rect, Placement.Right);
}
}
private
void
ShowContactCardWithOptions_Click(
object
sender, RoutedEventArgs e)
{
Contact contact = rootPage.CreateContactFromUserInput(EmailAddress, PhoneNumber);
if
(contact !=
null
)
{
// Show the contact card next to the button.
Rect rect = MainPage.GetElementRect(sender
as
FrameworkElement);
// Ask for the initial tab to be Phone.
ContactCardOptions options =
new
ContactCardOptions() { InitialTabKind = ContactCardTabKind.Phone };
// Show with default placement.
ContactManager.ShowContactCard(contact, rect, Placement.Default, options);
}
}
}
}
// Scenario2.Mini
using
System.Threading.Tasks;
using
Windows.ApplicationModel.Contacts;
using
Windows.Foundation;
using
Windows.UI.Popups;
using
Windows.UI.Xaml;
using
Windows.UI.Xaml.Controls;
using
Windows.UI.Xaml.Navigation;
namespace
SDKTemplate
{
public
sealed
partial
class
Scenario2_DelayMini : Page
{
private
MainPage rootPage = MainPage.Current;
public
Scenario2_DelayMini()
{
this
.InitializeComponent();
}
protected
override
void
OnNavigatedTo(NavigationEventArgs e)
{
if
(!ContactManager.IsShowDelayLoadedContactCardSupported())
{
NotSupportedWarning.Visibility = Visibility.Visible;
}
}
private
static
Contact CreatePlaceholderContact()
{
// Create contact object with small set of initial data to display.
Contact contact =
new
Contact();
contact.FirstName =
"Kim"
;
contact.LastName =
"Abercrombie"
;
ContactEmail email =
new
ContactEmail();
email.Address =
"
[email protected]
"
;
contact.Emails.Add(email);
return
contact;
}
private
async Task<Contact> DownloadContactDataAsync(Contact contact)
{
// Simulate the download latency by delaying the execution by 2 seconds.
await Task.Delay(2000);
if
(!DownloadSucceeded.IsChecked.Value)
{
return
null
;
}
// Add more data to the contact object.
ContactEmail workEmail =
new
ContactEmail();
workEmail.Address =
"
[email protected]
"
;
workEmail.Kind = ContactEmailKind.Work;
contact.Emails.Add(workEmail);
ContactPhone homePhone =
new
ContactPhone();
homePhone.Number =
"(444) 555-0101"
;
homePhone.Kind = ContactPhoneKind.Home;
contact.Phones.Add(homePhone);
ContactPhone workPhone =
new
ContactPhone();
workPhone.Number =
"(245) 555-0123"
;
workPhone.Kind = ContactPhoneKind.Work;
contact.Phones.Add(workPhone);
ContactPhone mobilePhone =
new
ContactPhone();
mobilePhone.Number =
"(921) 555-0187"
;
mobilePhone.Kind = ContactPhoneKind.Mobile;
contact.Phones.Add(mobilePhone);
ContactAddress address =
new
ContactAddress();
address.StreetAddress =
"123 Main St"
;
address.Locality =
"Redmond"
;
address.Region =
"WA"
;
address.Country =
"USA"
;
address.PostalCode =
"00000"
;
address.Kind = ContactAddressKind.Home;
contact.Addresses.Add(address);
return
contact;
}
private
async
void
ShowContactCard_Click(
object
sender, RoutedEventArgs e)
{
Contact contact = CreatePlaceholderContact();
// Show the contact card next to the button.
Rect rect = MainPage.GetElementRect(sender
as
FrameworkElement);
// The contact card placement can change when it is updated with more data. For improved user experience, specify placement
// of the card so that it has space to grow and will not need to be repositioned. In this case, default placement first places
// the card above the button because the card is small, but after the card is updated with more data, the operating system moves
// the card below the button to fit the card's expanded size. Specifying that the contact card is placed below at the beginning
// avoids this repositioning.
Placement placement = Placement.Below;
// For demonstration purposes, we ask for the Enterprise contact card.
ContactCardOptions options =
new
ContactCardOptions() { HeaderKind = ContactCardHeaderKind.Enterprise };
using
(ContactCardDelayedDataLoader dataLoader = ContactManager.ShowDelayLoadedContactCard(contact, rect, placement, options))
{
// Simulate downloading more data from the network for the contact.
this
.rootPage.NotifyUser(
"Simulating download..."
, NotifyType.StatusMessage);
Contact fullContact = await DownloadContactDataAsync(contact);
if
(fullContact !=
null
)
{
// Update the contact card with the full set of contact data.
dataLoader.SetData(fullContact);
this
.rootPage.NotifyUser(
"Contact has been updated with downloaded data."
, NotifyType.StatusMessage);
}
else
{
this
.rootPage.NotifyUser(
"No further information available."
, NotifyType.StatusMessage);
}
// The "using" statement will dispose the dataLoader for us.
}
}
}
}
// Scenario Full
using
Windows.ApplicationModel.Contacts;
using
Windows.Foundation;
using
Windows.UI.ViewManagement;
using
Windows.UI.Xaml;
using
Windows.UI.Xaml.Controls;
using
Windows.UI.Xaml.Navigation;
namespace
SDKTemplate
{
public
sealed
partial
class
Scenario3_Full : Page
{
private
MainPage rootPage = MainPage.Current;
public
Scenario3_Full()
{
this
.InitializeComponent();
}
private
void
ShowContactCard_Click(
object
sender, RoutedEventArgs e)
{
Contact contact = rootPage.CreateContactFromUserInput(EmailAddress, PhoneNumber);
if
(contact !=
null
)
{
// Try to share the screen half/half with the full contact card.
FullContactCardOptions options =
new
FullContactCardOptions();
options.DesiredRemainingView = ViewSizePreference.UseHalf;
// Show the full contact card.
ContactManager.ShowFullContactCard(contact, options);
}
}
}
}
UWP
Up Next
How to create contactcards in UWP App