DataPackage.SetDataProvider 不等待(与其文档状态不同) [英] DataPackage.SetDataProvider doesn't wait (unlike its documentation states)

查看:12
本文介绍了DataPackage.SetDataProvider 不等待(与其文档状态不同)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DataPackage.SetDataProvider 的文档 状态:

<块引用>

当您的应用...不想使用 SetDataProvider 方法时提供数据直到目标应用请求它.

但是当我运行以下代码时,它会立即调用回调方法.

static void CopyToClipboardReference(string s){DataPackage dataPackage = new DataPackage();参考 = s;dataPackage.SetDataProvider(StandardDataFormats.Text, CopyToClipboardAction);Clipboard.SetContent(dataPackage);}静态字符串引用;静态无效 CopyToClipboardAction(DataProviderRequest 请求){//立即调用!request.SetData(参考);}

当我将 StandardDataFormats.Text 更改为 StandardDataFormats.Html 时,它确实按预期工作(延迟渲染),但随后我没有粘贴"选项记事本等应用程序.

我如何让它等待文本,直到它从目标应用程序调用它为止,因为它根据其文档应该这样做?

另外:

DataTransfer.OperationCompleted 事件未引发.

解决方案

作为文档 备注部分:

<块引用>

当您的应用支持特定格式但不想在目标应用请求数据之前提供数据时,请使用 SetDataProvider 方法.

所以如果你想获取内容,你应该使用 DataPackageView 类并使用相应的方法来获取.作为 StandardDataFormats.Html 格式的示例,

当您复制 HTML 内容时:

static void CopyToClipboardReference(string s){DataPackage dataPackage = new DataPackage();参考 = s;dataPackage.SetDataProvider(StandardDataFormats.Html, CopyToClipboardAction);Clipboard.SetContent(dataPackage);}静态字符串引用;静态无效 CopyToClipboardAction(DataProviderRequest 请求){//立即调用!request.SetData(参考);}私有无效 CopyButton_Click(对象发送者,RoutedEventArgs e){CopyToClipboardReference("

加载html字符串.

");}

要获取内容,您应该调用 DataPackageView.GetHtmlFormatAsync 获取它然后 CopyToClipboardAction 回调将被触发.

async void PasteButton_Click(object sender, RoutedEventArgs e){var data = Windows.ApplicationModel.DataTransfer.Clipboard.GetContent();if (data.Contains(StandardDataFormats.Html)){字符串 htmlFormat = null;{htmlFormat = 等待 data.GetHtmlFormatAsync();}}}

但是对于 StandardDataFormats.Text 格式,CopyToClipboardAction 回调会引发两次,我不确定是否是设计使然,我会确认.>

另一方面,对于未引发 DataTransfer.OperationCompleted 事件的问题,您可以在另一个线程 (粘贴"操作后未引发OperationCompleted"事件)在粘贴处理程序方法中调用以下代码.>

dataPackageView.ReportOperationCompleted(DataPackageOperation.Copy);

DataPackage.SetDataProvider's documentation states:

Use the SetDataProvider method when your app ... does not want to supply the data until the target app requests it.

But when I run the following code it calls the callback method immediately.

static void CopyToClipboardReference(string s)
{
    DataPackage dataPackage = new DataPackage();
    reference = s;
    dataPackage.SetDataProvider(StandardDataFormats.Text, CopyToClipboardAction);
    Clipboard.SetContent(dataPackage);
}
static string reference;
static void CopyToClipboardAction(DataProviderRequest request)
{
    //Called immediately!
    request.SetData(reference);
}

When I change StandardDataFormats.Text to StandardDataFormats.Html it does work as expected (delayed rendering) but then I don't get an option for 'Paste' in applications such as Notepad.

How do I get it to wait for text until it's called from a target app as it is supposed to do according to its documentation?

Additionally:

The DataTransfer.OperationCompleted event is not raised.

解决方案

As the document Remarks part:

Use the SetDataProvider method when your app supports a specific format, but does not want to supply the data until the target app requests it.

So if you want to get the content, you should use DataPackageView Class and use the corresponding method to get it. As a example for the StandardDataFormats.Html format,

When you copy the HTML content:

static void CopyToClipboardReference(string s)
{
    DataPackage dataPackage = new DataPackage();

    reference = s;
    dataPackage.SetDataProvider(StandardDataFormats.Html, CopyToClipboardAction);
    Clipboard.SetContent(dataPackage);
}


static string reference;
static void CopyToClipboardAction(DataProviderRequest request)
{
    //Called immediately!
    request.SetData(reference);
}

private void CopyButton_Click(object sender, RoutedEventArgs e)
{
    CopyToClipboardReference("<Html><Body><h1>Load html string.<h1><Body></Html>");
}

To get the content, you should call the DataPackageView.GetHtmlFormatAsync to get it then the CopyToClipboardAction callback will be triggered.

async void PasteButton_Click(object sender, RoutedEventArgs e)
{
    var data = Windows.ApplicationModel.DataTransfer.Clipboard.GetContent();
    if (data.Contains(StandardDataFormats.Html))
    {
        string htmlFormat = null;
        {
            htmlFormat = await data.GetHtmlFormatAsync();
        }
    }
}

But as for the StandardDataFormats.Text format, the CopyToClipboardAction callback will raised twice, I am not sure if it is by design and I will confirm it.

On the other hand, for your issue about the DataTransfer.OperationCompleted event is not raised, you can see the detailed answer in your another thread ('OperationCompleted' event not raised after 'Paste' operation) to call the following code in your paste handler method.

dataPackageView.ReportOperationCompleted(DataPackageOperation.Copy);

这篇关于DataPackage.SetDataProvider 不等待(与其文档状态不同)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆