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

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

问题描述

DataPackage.SetDataProvider的文档状态:

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

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);
}

当我将 StandardDataFormats.Text 更改为 StandardDataFormats.Html 时,它确实可以正常工作(延迟渲染),但是我在其中没有粘贴"选项记事本等应用程序.

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?

其他:

DataTransfer.OperationCompleted 事件未引发.

推荐答案

作为文档

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

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,

当您复制HTML内容时:

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();
        }
    }
}

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

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.

另一方面,对于未引发有关 DataTransfer.OperationCompleted 事件的问题,您可以在另一个线程中看到详细的答案(),以在粘贴处理程序方法中调用以下代码.

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天全站免登陆