共享合同:如何从单个页面共享两种不同类型的数据?

时间:2015-06-24 06:43:18

标签: windows windows-phone-8.1

如何在源应用中使用共享合约从同一页面共享两种或更多种不同类型的内容(StorageFile和Link)?

DataTransferManager.ShowShareUI()触发单个事件处理程序,准备数据包以进行共享。

如何在同一页面中创建两个(或更多)事件处理程序,其中一个处理图像而另一个处理文本?

1 个答案:

答案 0 :(得分:1)

创建一个DataRequested处理程序并将多个数据对象添加到同一个DataRequest,而不是创建多个事件处理程序。然后,目标应用可以选择最合适的数据类型(您无法强制目标选择多种类型)。

private async void DataRequested(DataTransferManager sender, DataRequestedEventArgs e)
{
    DataRequest request = e.Request;
    request.Data.Properties.Title = "Share Link or StorageFile";
    request.Data.Properties.Description = "Lorem ipsum dolor sit amet";

    // Share some text
    request.Data.SetText("consectetur adipiscing elit");
    // Share a link
    request.Data.SetWebLink(new Uri("http://example.com"));

    // And share a file. 
    // A deferral to find the file asynchronously. It's not needed
    // if the file is already cached in a variable
    DataRequestDeferral deferral = request.GetDeferral();
    try
    {
        StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///assets/logo.png"));
        List<IStorageItem> items = new List<IStorageItem>();
        items.Add(file);
        request.Data.SetStorageItems(items);
    }
    finally
    {
        deferral.Complete();
    }
}
相关问题