4

要求:DataTransferManager在 Windows 10 中使用 Facebook 分享文本和图像。

问题:无法共享图像。

下面显示的是我使用的代码,

 private async void DataTransferManager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            DataRequestDeferral deferral = args.Request.GetDeferral();
            args.Request.Data.Properties.Title = "Sharing sample";
            args.Request.Data.SetText("Testing share in universal app");
            var imageUri = "http://cdn.vrworld.com/wp-content/uploads/2015/01/microsoft-announces-windows-10_ahab.1920.jpg";

            //var storageFile = await StorageFile.CreateStreamedFileFromUriAsync("ShareFile", new Uri(imageUri), null);
            //List<IStorageItem> storageItems = new List<IStorageItem>();
            //storageItems.Add(storageFile);
            //args.Request.Data.SetStorageItems(storageItems);

            args.Request.Data.SetBitmap(Windows.Storage.Streams.RandomAccessStreamReference.CreateFromUri(new Uri(imageUri)));
            deferral.Complete();
        }

当我使用SetBitmap方法时,只共享标题和文本。图像既不会显示在共享面板中,也不会共享到目标应用程序。

当我使用SetStorageItems(见注释代码)时,没有任何项目是共享的。默认的“你在想什么”文本显示在共享面板上。

任何反馈表示赞赏,谢谢!

4

2 回答 2

3

不幸的是,不支持共享 URI 流文件。以下是我将如何去做:

  1. 当用户单击共享按钮时,开始下载文件并显示某种进度(如果文件很大)。当然,您也可以预先下载文件。设置一个StorageFile包含该文件的实例。
  2. 调用DataTransferManager.ShowShareUI
  3. 在您的DataRequested处理程序中,用于SetStorageItems共享StorageFile实例。
于 2015-05-13T01:05:13.117 回答
-3

我想你指的是 UWP 中的 Share Target 你可以参考这个 URL https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/ShareSource

此示例演示了一个应用程序如何与另一个应用程序共享内容。此示例使用 Windows.ApplicationModel.DataTransfer 命名空间中的类。您可能想要更详细地查看的一些类是用于启动共享操作的 DataTransferManager 类和用于打包内容的 DataPackage 类。因为每个共享方案通常涉及两个应用程序 - 源应用程序和接收内容的目标应用程序 - 我们建议您在安装和运行共享内容目标应用程序示例时安装和部署此应用程序。通过这种方式,您可以看到共享是如何从头到尾进行的。

此示例介绍如何以各种格式共享内容,包括:

1.Text 2.Web 链接 3.Application 链接 4.Images 5.Files 6.延迟渲染文件 7.HTML 内容 8.自定义数据

protected override bool GetShareContent(DataRequest request)
    {
        bool succeeded = false;

        if (this.imageFile != null)
        {
            DataPackage requestData = request.Data;
            requestData.Properties.Title = TitleInputBox.Text;
            requestData.Properties.Description = DescriptionInputBox.Text; // The description is optional.
            requestData.Properties.ContentSourceApplicationLink = ApplicationLink;

            // It's recommended to use both SetBitmap and SetStorageItems for sharing a single image
            // since the target app may only support one or the other.

            List<IStorageItem> imageItems = new List<IStorageItem>();
            imageItems.Add(this.imageFile);
            requestData.SetStorageItems(imageItems);

            RandomAccessStreamReference imageStreamRef = RandomAccessStreamReference.CreateFromFile(this.imageFile);
            requestData.Properties.Thumbnail = imageStreamRef;
            requestData.SetBitmap(imageStreamRef);
            succeeded = true;
        }
        else
        {
            request.FailWithDisplayText("Select an image you would like to share and try again.");
        }
        return succeeded;
    }
于 2015-10-31T09:48:48.370 回答