'OperationCompleted' event not raised afte

2019-09-02 12:25发布

问题:

The OperationCompleted event is supposed to be raised at the end of a Clipboard Paste operation as mentioned in DataPackage:

OperationCompleted Occurs when a paste operation is completed.

It's not.

Why? / What's a workaround?

Code:

static void CopyToClipboard(string s)
{
    DataPackage dataPackage = new DataPackage();
    dataPackage.SetText(s);
    dataPackage.OperationCompleted += DataPackage_OperationCompleted1;
    Clipboard.SetContent(dataPackage);
}
static void DataPackage_OperationCompleted1(DataPackage sender, OperationCompletedEventArgs args)
{
    throw new NotImplementedException();
}

回答1:

Please see the Remarks section on the document.

This event occurs when a user or program pastes content from the Clipboard. If your app is using the DataPackage for share operations, you do not have to handle this event.

Then, in your paste handler method, you would need to use dataPackageView.ReportOperationCompleted() method to inform the system that your app is finished using the DataPackageView object.

I used the official Clipboard code sample to test.

In this line, I added the following code:

dataPackageView.ReportOperationCompleted(DataPackageOperation.Copy);

After that, the DataPackage's OperationCompleted event would be fired.