“我的人脉”共享My People sharing

利用“我的人脉”功能,用户可以将联系人固定到其任务栏,这样,他们可以从 Windows 中的任何地方轻松地保持联系,而与他们进行联系所使用的应用程序无关。The My People feature allows users to pin contacts to their taskbar, enabling them to stay in touch easily from anywhere in Windows, no matter what application they are connected by. 现在,通过将文件从文件资源管理器拖到“我的人脉”固定区域,用户可以与他们固定的联系人分享内容。Now users can share content with their pinned contacts by dragging files from the File Explorer to their My People pin. 他们还可以通过标准共享超级按钮与 Windows 联系人存储中的任何联系人共享。They can also share to any contacts in the Windows contact store via the standard share charm. 请继续阅读,了解如何将应用程序启用为“我的人脉”共享目标。Keep reading to learn how to enable your application as a My People sharing target.

“我的人脉”共享面板

要求Requirements

概述Overview

你必须执行以下三个步骤,才能将应用程序启用为“我的人脉”共享目标:There are three steps you must take to enable your application as a My People sharing target:

  1. 声明对应用程序清单中的 shareTarget 激活合约提供支持。Declare support for the shareTarget activation contract in your application manifest.
  2. 为用户可与之共用应用的联系人添加注释。Annotate the contacts that the users can share to using your app.
  3. 支持同时运行应用程序的多个实例。Support multiple instances of the application running at the same time. 用户必须能够与完整版本的应用程序进行交互,并且同时也可以使用应用程序与其他人共享。Users must be able to interact with a full version of your application while also using it to share with others. 他们可以同时在多个共享窗口中使用该应用程序。They may use it in multiple share windows at once. 为了对此提供支持,应用程序需要能够同时运行多个视图。To support this, your application needs to be able to run multiple views simultaneously. 若要了解如何执行此操作,请参阅文章“显示应用的多个视图”To learn how to do this, see the article "show multiple views for an app".

当你完成此操作后,应用程序将在“我的人脉”共享窗口中显示为共享目标,可以通过以下两种方式启动共享目标:When you’ve done this, your application will appear as a share target in the My People share window, which can be launched in two ways:

  1. 通过共享超级按钮选择联系人。A contact is chosen via the share charm.
  2. 将文件拖放到固定到任务栏上的联系人。File(s) are dragged and dropped on a contact pinned to the taskbar.

声明对“共享”合约提供支持Declaring support for the share contract

若要声明支持以应用程序作为共享目标,请首先在 Visual Studio 中打开应用程序。To declare support for your application as a share target, first open your application in Visual Studio. 解决方案资源管理器中,右键单击 package.appxmanifest 并选择打开方式From the Solution Explorer, right click Package.appxmanifest and select Open With. 从菜单中,选择 XML (文本)编辑器,然后单击确定From the menu, select XML (Text) Editor and click OK. 然后,对清单进行以下更改:Then, make the following changes to the manifest:

之前Before

<Applications>
    <Application Id="MyApp"
      Executable="$targetnametoken$.exe"
      EntryPoint="My.App">
    </Application>
</Applications>

之后After

<Applications>
    <Application Id="MyApp"
      Executable="$targetnametoken$.exe"
      EntryPoint="My.App">
        <Extensions>
            <uap:Extension Category="windows.shareTarget">
                <uap:ShareTarget Description="Share with MyApp">
                    <uap:SupportedFileTypes>
                        <uap:SupportsAnyFileType/>
                    </uap:SupportedFileTypes>
                    <uap:DataFormat>Text</uap:DataFormat>
                    <uap:DataFormat>Bitmap</uap:DataFormat>
                    <uap:DataFormat>Html</uap:DataFormat>
                    <uap:DataFormat>StorageItems</uap:DataFormat>
                    <uap:DataFormat>URI</uap:DataFormat>
                </uap:ShareTarget>
            </uap:Extension>
         </Extensions>
    </Application>
</Applications>

此代码将添加对所有文件和数据格式的支持,但是你可以选择指定所支持的文件类型和数据格式(请参阅 ShareTarget 类文档了解更多详细信息)。This code adds support for all files and data formats, but you can choose to specify what files types and data formats are supported (see ShareTarget class documentation for more details).

为联系人添加注释Annotating contacts

若要允许“我的人脉”共享窗口将应用程序显示为联系人的共享目标,你需要将联系人写入 Windows 联系人存储中。To allow the My People share window to show your application as a share target for your contacts, you need to write them to the Windows contact store. 若要了解如何编写联系人,请参阅联系人卡片集成示例To learn how to write contacts, see the Contact Card Integration sample.

为了在与联系人共享时使你的应用程序显示为“我的人脉”共享目标,应用程序必须为该联系人编写注释。For your application to appear as a My People share target when sharing to a contact, it must write an annotation to that contact. 注释是应用程序中与联系人相关联的一些数据。Annotations are pieces of data from your application that are associated with a contact. 注释必须包含与 ProviderProperties 成员中所需视图相对应的可激活的类,并且声明对 Share 操作提供支持。The annotation must contain the activatable class corresponding to your desired view in its ProviderProperties member, and declare support for the Share operation.

你可以在应用运行过程中随时为联系人添加注释,但通常,在将联系人添加到 Windows 联系人存储后,你应该立即为联系人添加注释。You can annotate contacts at any point while your app is running, but generally you should annotate contacts as soon as they are added to the Windows contact store.

if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 5))
{
    // Create a new contact annotation
    ContactAnnotation annotation = new ContactAnnotation();
    annotation.ContactId = myContact.Id;

    // Add appId and Share support to the annotation
    String appId = "MyApp_vqvv5s4y3scbg!App";
    annotation.ProviderProperties.Add("ContactShareAppID", appId);
    annotation.SupportedOperations = ContactAnnotationOperations::Share;

    // Save annotation to contact annotation list
    // Windows.ApplicationModel.Contacts.ContactAnnotationList 
    await contactAnnotationList.TrySaveAnnotationAsync(annotation);
}

“appId”是后跟“!”The “appId” is the Package Family Name, followed by ‘!’ 和可激活的类 ID 的包系列名称。and the Activatable Class ID. 若要查找你的包系列名称,请使用默认编辑器打开 Package.appxmanifest,然后查找“Packaging”选项卡。在这里,“App”是指与“共享目标”视图对应的可激活类。To find your Package Family Name, open Package.appxmanifest using the default editor, and look in the “Packaging” tab. Here, “App” is the Activatable Class corresponding to the Share Target view.

作为“我的人脉”共享目标运行Running as a My People share target

最后,若要运行该应用,请替代应用主类中的 OnShareTargetActivated 方法,以处理共享目标激活。Finally, to run the app, override the OnShareTargetActivated method in your app’s main class to handle the share target activation. ShareTargetActivatedEventArgs.ShareOperation.Contacts 属性将包含与之共享的联系人,如果这是标准共享操作(不是“我的人脉”共享),则将为空。The ShareTargetActivatedEventArgs.ShareOperation.Contacts property will contain the contact(s) that are being shared to, or will be empty if this is a standard share operation (not a My People share).

protected override void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
{
    bool isPeopleShare = false;
    if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 5))
    {
        // Make sure the current OS version includes the My People feature before
        // accessing the ShareOperation.Contacts property
        isPeopleShare = (args.ShareOperation.Contacts.Count > 0);
    }

    if (isPeopleShare)
    {
        // Show share UI for MyPeople contact(s)
    }
    else
    {
        // Show standard share UI for unpinned contacts
    }
}

另请参阅See also