1

我正在创建一个共享 InkManager 笔划的应用程序,方法是首先将 inkmanager 笔划保存到本地设置,然后将其作为流读取并共享。

现在 HTML 内容正在共享,但电子邮件中没有附加图像。如果我评论代码以共享 html,则在通过电子邮件应用程序共享时,图像会被附加。但两者都不会立即共享。我怎样才能做到这一点???

代码:

DataPackage requestData = request.Data;
                requestData.Properties.Title =        this.PageViewModel.JobInformationDetail.JobNumber;
                requestData.Properties.Description = this.PageViewModel.JobInformationDetail.CustomerSignatureName;
                //requestData.SetText("Sample Text");

                RandomAccessStreamReference signatureStream = await GetInkManagerStream();

                if (signatureStream != null)
                {
                    requestData.SetBitmap(signatureStream);
                }

                requestData.SetHtmlFormat(Windows.ApplicationModel.DataTransfer.HtmlFormatHelper.CreateHtmlFormat(this.GetMailDescription()));

将 InkManager 转换为流

private async Task<RandomAccessStreamReference> GetInkManagerStream()
    {
        IRandomAccessStream signatureStream = null;

        StorageFile myMerge = await ApplicationData.Current.LocalFolder.CreateFileAsync("sign.png", CreationCollisionOption.OpenIfExists);
        IOutputStream signature = await myMerge.OpenAsync(FileAccessMode.ReadWrite);

        if (signature != null)
        {
            await _inkManager.SaveAsync(signature);
            signature.Dispose();
        }

        signatureStream = await myMerge.OpenAsync(FileAccessMode.ReadWrite);
        RandomAccessStreamReference streamRef = RandomAccessStreamReference.CreateFromStream(signatureStream);
        signatureStream.Dispose();

        return streamRef;
    }
4

1 回答 1

2

该问题是由于在事件处理程序中等待async方法引起的。DataRequested如果你这样做,你需要使用DataRequestReferral

private async void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{
    var deferral = args.Request.GetDeferral();

    // the rest of your method, including the awaiting call

    deferral.Complete();
}

通过这样做,您告诉调用者它需要等待异步调用完成才能继续。最后,您通过调用deferral.Complete(). 如果没有它,调用者会在您实际设置位图之前继续,因此会出现错误。

编辑:

不过,Windows 8 Mail 应用程序不是像这样共享的图像的共享目标。要使 Mail 应用程序可用,您需要将图像共享为存储项目:

var file = await ApplicationData.Current.LocalFolder.GetFileAsync(@"sign.png");
var imageItems = new List<IStorageItem>();
imageItems.Add(file);
requestData.SetStorageItems(imageItems);

现在邮件将显示为可用目标之一。当您共享图像时,最好同时使用两者SetBitmap()SetStorageItems()获得更多可用的共享目标,因为并非所有应用程序都支持这两种数据类型。

编辑2:

Also the Mail app only picks up one type of shared content, either HTML or StorageItems (if no HTML is provided) as you've already noticed. The only workaround I know of, is to embed the images into the HTML content:

var resourceName = "logo.png";
var html = String.Format("<p>HTML content</p><img src='{0}'/>", resourceName);
requestData.ResourceMap[resourceName] = RandomAccessStreamReference.CreateFromUri(
    new Uri("ms-appdata:///local/sign.png"));
requestData.SetHtmlFormat(HtmlFormatHelper.CreateHtmlFormat(html));

You can read more about this approach in a blog post by Mike Taulty.

于 2013-03-30T06:10:26.977 回答