【问题标题】:Unable to share image using DataTransferManager in Windows 10无法在 Windows 10 中使用 DataTransferManager 共享图像
【发布时间】:2016-06-20 16:12:01
【问题描述】:

要求:在 Windows 10 中使用 DataTransferManager 将文本和图像分享到 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(见注释代码)时,没有任何项目是共享的。共享面板上显示默认的“您的想法”文本。

感谢任何反馈,谢谢!

【问题讨论】:

  • 你试过分享到其他应用吗? Facebook 可能不支持这些数据类型。
  • @Peter Torr MSFT - 我尝试分享到 twitter,但图像也没有出现。我遇到的另一个问题是默认邮件应用和 google+ 没有显示在共享窗格的应用列表中。

标签: c# facebook windows-10 fileshare


【解决方案1】:

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

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

【讨论】:

【解决方案2】:

我认为您指的是 UWP 中的共享目标 你可以参考这个网址 https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/ShareSource

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

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

1.文本 2.网页链接 3.申请链接 4.图像 5.文件 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;
    }

【讨论】:

  • 链接中的另一个复制粘贴答案。这可能是我在你的答案中找到的第 10 个。请考虑如何更恰当地回答你的提问者——或者更确切地说——我会说更个人化。复制粘贴的答案只对 cme​​ts 有价值。
  • 源代码更新
  • 如果您从其他地方复制/粘贴代码,您必须明确说明它不是由您编写的。仅仅添加一个链接是不够的。使用quote formatting(以&gt; 开头的行)将您的文字与您复制的文字区分开来。
猜你喜欢
  • 2021-01-19
  • 1970-01-01
  • 2022-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多