将你的应用与联系人卡片上的操作关联起来

你的应用可以显示在联系人卡片或迷你联系人卡片上的操作旁边。 用户可以选择你的应用执行某项操作,如打开个人资料页面、打电话或发送消息。

联系人卡片和迷你联系人卡片

若要开始,请查找现有联系人或创建新联系人。 接下来,创建 批注 和几个包清单条目来描述应用支持的操作。 然后,编写执行操作的代码。

有关更完整的示例,请参阅 联系人卡片集成示例

查找或创建联系人

如果你的应用帮助他人与其他人联系,请搜索 Windows 查找联系人,然后批注联系人。 如果你的应用管理联系人,你可以将它们添加到 Windows 联系人列表,然后批注它们。

查找联系人

使用姓名、电子邮件地址或电话号码查找联系人。

ContactStore contactStore = await ContactManager.RequestStoreAsync();

IReadOnlyList<Contact> contacts = null;

contacts = await contactStore.FindContactsAsync(emailAddress);

Contact contact = contacts[0];

创建联系人

如果你的应用更像是通讯簿,请创建联系人,然后将其添加到联系人列表中。

Contact contact = new Contact();
contact.FirstName = "TestContact";

ContactEmail email = new ContactEmail();
email.Address = "TestContact@contoso.com";
email.Kind = ContactEmailKind.Other;
contact.Emails.Add(email);

ContactPhone phone = new ContactPhone();
phone.Number = "4255550101";
phone.Kind = ContactPhoneKind.Mobile;
contact.Phones.Add(phone);

ContactStore store = await
    ContactManager.RequestStoreAsync(ContactStoreAccessType.AppContactsReadWrite);

ContactList contactList;

IReadOnlyList<ContactList> contactLists = await store.FindContactListsAsync();

if (0 == contactLists.Count)
    contactList = await store.CreateContactListAsync("TestContactList");
else
    contactList = contactLists[0];

await contactList.SaveContactAsync(contact);

使用批注标记每个联系人

使用应用可以执行的操作列表(例如视频呼叫和消息)标记每个联系人。

然后,将联系人的 ID 关联到应用在内部用来标识该用户的 ID。

ContactAnnotationStore annotationStore = await
   ContactManager.RequestAnnotationStoreAsync(ContactAnnotationStoreAccessType.AppAnnotationsReadWrite);

ContactAnnotationList annotationList;

IReadOnlyList<ContactAnnotationList> annotationLists = await annotationStore.FindAnnotationListsAsync();
if (0 == annotationLists.Count)
    annotationList = await annotationStore.CreateAnnotationListAsync();
else
    annotationList = annotationLists[0];

ContactAnnotation annotation = new ContactAnnotation();
annotation.ContactId = contact.Id;
annotation.RemoteId = "user22";

annotation.SupportedOperations = ContactAnnotationOperations.Message |
  ContactAnnotationOperations.AudioCall |
  ContactAnnotationOperations.VideoCall |
 ContactAnnotationOperations.ContactProfile;

await annotationList.TrySaveAnnotationAsync(annotation);

注册每个操作

在包清单中,注册注释中列出的每个操作。

通过将协议处理程序添加到清单的元素进行 Extensions 注册。

<Extensions>
  <uap:Extension Category="windows.protocol">
    <uap:Protocol Name="ms-contact-profile">
      <uap:DisplayName>TestProfileApp</uap:DisplayName>
    </uap:Protocol>
  </uap:Extension>
  <uap:Extension Category="windows.protocol">
    <uap:Protocol Name="ms-ipmessaging">
      <uap:DisplayName>TestMsgApp</uap:DisplayName>
    </uap:Protocol>
  </uap:Extension>
  <uap:Extension Category="windows.protocol">
    <uap:Protocol Name="ms-voip-video">
      <uap:DisplayName>TestVideoApp</uap:DisplayName>
    </uap:Protocol>
  </uap:Extension>
  <uap:Extension Category="windows.protocol">
    <uap:Protocol Name="ms-voip-call">
      <uap:DisplayName>TestCallApp</uap:DisplayName>
    </uap:Protocol>
  </uap:Extension>
</Extensions>

还可以在 Visual Studio 的清单设计器的“ 声明”选项卡中添加这些内容。

清单设计器的“声明”选项卡

在联系人卡片中的操作旁边查找你的应用

打开“人员”应用。 你的应用显示在注释和包清单中指定的每个操作(操作)旁边。

联系人卡片

如果用户为某个操作选择你的应用,则当用户下次打开联系人卡片时,该应用显示为该操作的默认应用。

在迷你联系人卡片中的操作旁边查找你的应用

在迷你联系人卡片中,你的应用将显示在表示操作的选项卡中。

迷你联系人卡片

邮件应用等应用打开迷你联系人卡片。 你的应用也可以打开它们。 此代码演示如何执行此操作。

public async void OpenContactCard(object sender, RoutedEventArgs e)
{
    // Get the selection rect of the button pressed to show contact card.
    FrameworkElement element = (FrameworkElement)sender;

    Windows.UI.Xaml.Media.GeneralTransform buttonTransform = element.TransformToVisual(null);
    Windows.Foundation.Point point = buttonTransform.TransformPoint(new Windows.Foundation.Point());
    Windows.Foundation.Rect rect =
        new Windows.Foundation.Rect(point, new Windows.Foundation.Size(element.ActualWidth, element.ActualHeight));

   // helper method to find a contact just for illustrative purposes.
    Contact contact = await findContact("contoso@contoso.com");

    ContactManager.ShowContactCard(contact, rect, Windows.UI.Popups.Placement.Default);

}

若要查看包含迷你联系人卡片的更多示例,请参阅 “联系人卡片”示例

与联系人卡片一样,每个选项卡都会记住用户上次使用的应用,以便他们可以轻松返回到应用。

当用户在联系人卡片中选择你的应用时执行操作

重写 App.cs 文件中的 Application.OnActivated 方法,并将用户导航到应用中的页面。 联系人卡片集成示例显示了执行此操作的一种方法。

在页面的代码隐藏文件中,重写 Page.OnNavigatedTo 方法。 联系人卡片传递此方法的操作名称和用户的 ID。

若要启动视频或音频呼叫,请参阅此示例: VoIP 示例。 可在 WIndows.ApplicationModel.Calls 命名空间中找到完整的 API。

若要促进消息传送,请参阅 Windows.ApplicationModel.Chat 命名空间。

还可以启动另一个应用。 这就是此代码的作用。

protected override async void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    var args = e.Parameter as ProtocolActivatedEventArgs;
    // Display the result of the protocol activation if we got here as a result of being activated for a protocol.

    if (args != null)
    {
        var options = new Windows.System.LauncherOptions();
        options.DisplayApplicationPicker = true;

        options.TargetApplicationPackageFamilyName = "ContosoApp";

        string launchString = args.uri.Scheme + ":" + args.uri.Query;
        var launchUri = new Uri(launchString);
        await Windows.System.Launcher.LaunchUriAsync(launchUri, options);
    }
}

args.uri.scheme 属性包含操作的名称,属性 args.uri.Query 包含用户的 ID。