Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

c# - DataPackage.SetDataProvider doesn't wait (unlike its documentation states)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Clipboard is a bit different from other sharing options because it is a system-wide feature and it can be used quite anywhere. I imagine that is the reason while the CopyTOClipboardAction is executed immediately because the user might want to use the clipboard immediately as well.

This also makes sense, because the user could want to set the clipboard contents and then close the app before pasting. This really means the data must be there because the system couldn't set the contents later anymore.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...