How to create contactcards in UWP App

  1. //MainPage1:  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using Windows.ApplicationModel.Contacts;  
  5. using Windows.Foundation;  
  6. using Windows.UI.Xaml;  
  7. using Windows.UI.Xaml.Controls;  
  8. namespace SDKTemplate  
  9. {  
  10. public partial class MainPage : Page  
  11. {  
  12. public const string FEATURE_NAME = "Contact cards C# sample";  
  13. List<Scenario> scenarios = new List<Scenario>  
  14. {  
  15. new Scenario() { Title="Show mini contact card", ClassType=typeof(Scenario1_Mini)},  
  16. new Scenario() { Title="Show mini contact card with delayed information", ClassType=typeof(Scenario2_DelayMini)},  
  17. new Scenario() { Title="Show full contact card", ClassType=typeof(Scenario3_Full)},  
  18. };  
  19. public Contact CreateContactFromUserInput(TextBox EmailAddress, TextBox PhoneNumber)  
  20. {  
  21. if (EmailAddress.Text.Length == 0 && PhoneNumber.Text.Length == 0)  
  22. {  
  23. NotifyUser("You must enter an email address and/or phone number.", NotifyType.ErrorMessage);  
  24. return null;  
  25. }  
  26. Contact contact = new Contact();  
  27. // Maximum length for email address is 321, enforced by XAML markup.  
  28. if (EmailAddress.Text.Length > 0)  
  29. {  
  30. ContactEmail email = new ContactEmail() { Address = EmailAddress.Text };  
  31. contact.Emails.Add(email);  
  32. }  
  33. // Maximum length for phone number is 50, enforced by XAML markup.  
  34. if (PhoneNumber.Text.Length > 0)  
  35. {  
  36. ContactPhone phone = new ContactPhone() { Number = PhoneNumber.Text };  
  37. contact.Phones.Add(phone);  
  38. }  
  39. return contact;  
  40. }  
  41. public static Rect GetElementRect(FrameworkElement element)  
  42. {  
  43. Windows.UI.Xaml.Media.GeneralTransform buttonTransform = element.TransformToVisual(null);  
  44. Point point = buttonTransform.TransformPoint(new Point());  
  45. return new Rect(point, new Size(element.ActualWidth, element.ActualHeight));  
  46. }  
  47. }  
  48. public class Scenario  
  49. {  
  50. public string Title { getset; }  
  51. public Type ClassType { getset; }  
  52. }  
  53. }  
  54. //Scenario_Mini1  
  55. using Windows.Foundation;  
  56. using Windows.UI.Popups;  
  57. using Windows.UI.Xaml;  
  58. using Windows.UI.Xaml.Controls;  
  59. using Windows.UI.Xaml.Navigation;  
  60. namespace SDKTemplate  
  61. {  
  62. public sealed partial class Scenario1_Mini : Page  
  63. {  
  64. private MainPage rootPage = MainPage.Current;  
  65. public Scenario1_Mini()  
  66. {  
  67. this.InitializeComponent();  
  68. }  
  69. protected override void OnNavigatedTo(NavigationEventArgs e)  
  70. {  
  71. if (!ContactManager.IsShowContactCardSupported())  
  72. {  
  73. NotSupportedWarning.Visibility = Visibility.Visible;  
  74. }  
  75. }  
  76. private void ShowContactCard_Click(object sender, RoutedEventArgs e)  
  77. {  
  78. Contact contact = rootPage.CreateContactFromUserInput(EmailAddress, PhoneNumber);  
  79. if (contact != null)  
  80. {  
  81. // Show the contact card next to the button.  
  82. Rect rect = MainPage.GetElementRect(sender as FrameworkElement);  
  83. // Show with default placement.  
  84. ContactManager.ShowContactCard(contact, rect);  
  85. }  
  86. }  
  87. private void ShowContactCardWithPlacement_Click(object sender, RoutedEventArgs e)  
  88. {  
  89. Contact contact = rootPage.CreateContactFromUserInput(EmailAddress, PhoneNumber);  
  90. if (contact != null)  
  91. {  
  92. // Show the contact card next to the button.  
  93. Rect rect = MainPage.GetElementRect(sender as FrameworkElement);  
  94. // Show with preferred placement to the right.  
  95. ContactManager.ShowContactCard(contact, rect, Placement.Right);  
  96. }  
  97. }  
  98. private void ShowContactCardWithOptions_Click(object sender, RoutedEventArgs e)  
  99. {  
  100. Contact contact = rootPage.CreateContactFromUserInput(EmailAddress, PhoneNumber);  
  101. if (contact != null)  
  102. {  
  103. // Show the contact card next to the button.  
  104. Rect rect = MainPage.GetElementRect(sender as FrameworkElement);  
  105. // Ask for the initial tab to be Phone.  
  106. ContactCardOptions options = new ContactCardOptions() { InitialTabKind = ContactCardTabKind.Phone };  
  107. // Show with default placement.  
  108. ContactManager.ShowContactCard(contact, rect, Placement.Default, options);  
  109. }  
  110. }  
  111. }  
  112. }  
  113. // Scenario2.Mini  
  114. using System.Threading.Tasks;  
  115. using Windows.ApplicationModel.Contacts;  
  116. using Windows.Foundation;  
  117. using Windows.UI.Popups;  
  118. using Windows.UI.Xaml;  
  119. using Windows.UI.Xaml.Controls;  
  120. using Windows.UI.Xaml.Navigation;  
  121. namespace SDKTemplate  
  122. {  
  123. public sealed partial class Scenario2_DelayMini : Page  
  124. {  
  125. private MainPage rootPage = MainPage.Current;  
  126. public Scenario2_DelayMini()  
  127. {  
  128. this.InitializeComponent();  
  129. }  
  130. protected override void OnNavigatedTo(NavigationEventArgs e)  
  131. {  
  132. if (!ContactManager.IsShowDelayLoadedContactCardSupported())  
  133. {  
  134. NotSupportedWarning.Visibility = Visibility.Visible;  
  135. }  
  136. }  
  137. private static Contact CreatePlaceholderContact()  
  138. {  
  139. // Create contact object with small set of initial data to display.  
  140. Contact contact = new Contact();  
  141. contact.FirstName = "Kim";  
  142. contact.LastName = "Abercrombie";  
  143. ContactEmail email = new ContactEmail();  
  144. email.Address = "[email protected]";  
  145. contact.Emails.Add(email);  
  146. return contact;  
  147. }  
  148. private async Task<Contact> DownloadContactDataAsync(Contact contact)  
  149. {  
  150. // Simulate the download latency by delaying the execution by 2 seconds.  
  151. await Task.Delay(2000);  
  152. if (!DownloadSucceeded.IsChecked.Value)  
  153. {  
  154. return null;  
  155. }  
  156. // Add more data to the contact object.  
  157. ContactEmail workEmail = new ContactEmail();  
  158. workEmail.Address = "[email protected]";  
  159. workEmail.Kind = ContactEmailKind.Work;  
  160. contact.Emails.Add(workEmail);  
  161. ContactPhone homePhone = new ContactPhone();  
  162. homePhone.Number = "(444) 555-0101";  
  163. homePhone.Kind = ContactPhoneKind.Home;  
  164. contact.Phones.Add(homePhone);  
  165. ContactPhone workPhone = new ContactPhone();  
  166. workPhone.Number = "(245) 555-0123";  
  167. workPhone.Kind = ContactPhoneKind.Work;  
  168. contact.Phones.Add(workPhone);  
  169. ContactPhone mobilePhone = new ContactPhone();  
  170. mobilePhone.Number = "(921) 555-0187";  
  171. mobilePhone.Kind = ContactPhoneKind.Mobile;  
  172. contact.Phones.Add(mobilePhone);  
  173. ContactAddress address = new ContactAddress();  
  174. address.StreetAddress = "123 Main St";  
  175. address.Locality = "Redmond";  
  176. address.Region = "WA";  
  177. address.Country = "USA";  
  178. address.PostalCode = "00000";  
  179. address.Kind = ContactAddressKind.Home;  
  180. contact.Addresses.Add(address);  
  181. return contact;  
  182. }  
  183. private async void ShowContactCard_Click(object sender, RoutedEventArgs e)  
  184. {  
  185. Contact contact = CreatePlaceholderContact();  
  186. // Show the contact card next to the button.  
  187. Rect rect = MainPage.GetElementRect(sender as FrameworkElement);  
  188. // The contact card placement can change when it is updated with more data. For improved user experience, specify placement  
  189. // of the card so that it has space to grow and will not need to be repositioned. In this case, default placement first places  
  190. // the card above the button because the card is small, but after the card is updated with more data, the operating system moves  
  191. // the card below the button to fit the card's expanded size. Specifying that the contact card is placed below at the beginning  
  192. // avoids this repositioning.  
  193. Placement placement = Placement.Below;  
  194. // For demonstration purposes, we ask for the Enterprise contact card.  
  195. ContactCardOptions options = new ContactCardOptions() { HeaderKind = ContactCardHeaderKind.Enterprise };  
  196. using (ContactCardDelayedDataLoader dataLoader = ContactManager.ShowDelayLoadedContactCard(contact, rect, placement, options))  
  197. {  
  198. // Simulate downloading more data from the network for the contact.  
  199. this.rootPage.NotifyUser("Simulating download...", NotifyType.StatusMessage);  
  200. Contact fullContact = await DownloadContactDataAsync(contact);  
  201. if (fullContact != null)  
  202. {  
  203. // Update the contact card with the full set of contact data.  
  204. dataLoader.SetData(fullContact);  
  205. this.rootPage.NotifyUser("Contact has been updated with downloaded data.", NotifyType.StatusMessage);  
  206. }  
  207. else  
  208. {  
  209. this.rootPage.NotifyUser("No further information available.", NotifyType.StatusMessage);  
  210. }  
  211. // The "using" statement will dispose the dataLoader for us.  
  212. }  
  213. }  
  214. }  
  215. }  
  216. // Scenario Full  
  217. using Windows.ApplicationModel.Contacts;  
  218. using Windows.Foundation;  
  219. using Windows.UI.ViewManagement;  
  220. using Windows.UI.Xaml;  
  221. using Windows.UI.Xaml.Controls;  
  222. using Windows.UI.Xaml.Navigation;  
  223. namespace SDKTemplate  
  224. {  
  225. public sealed partial class Scenario3_Full : Page  
  226. {  
  227. private MainPage rootPage = MainPage.Current;  
  228. public Scenario3_Full()  
  229. {  
  230. this.InitializeComponent();  
  231. }  
  232. private void ShowContactCard_Click(object sender, RoutedEventArgs e)  
  233. {  
  234. Contact contact = rootPage.CreateContactFromUserInput(EmailAddress, PhoneNumber);  
  235. if (contact != null)  
  236. {  
  237. // Try to share the screen half/half with the full contact card.  
  238. FullContactCardOptions options = new FullContactCardOptions();  
  239. options.DesiredRemainingView = ViewSizePreference.UseHalf;  
  240. // Show the full contact card.  
  241. ContactManager.ShowFullContactCard(contact, options);  
  242. }  
  243. }  
  244. }  
  245. }