无法在Windows 10中使用DataTransferManager共享图像

时间:2015-05-05 11:25:55

标签: c# facebook windows-10 fileshare

要求:使用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时(请参阅注释代码),不会共享任何项目。共享面板上会显示默认的“您的想法”文字。

感谢您的任何反馈,谢谢!

2 个答案:

答案 0 :(得分:3)

遗憾的是,不支持共享URI流式文件。以下是我将如何做到这一点:

  1. 当用户单击共享按钮时,开始下载文件 并且如果它是一个大文件,则显示某种进展。你也可以 当然预先下载文件。设置StorageFile实例 包含文件。
  2. 致电 DataTransferManager.ShowShareUI
  3. DataRequested处理程序中,使用SetStorageItems分享StorageFile个实例。

答案 1 :(得分:-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.应用程序链接 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;
    }