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

前端 未结 2 1093
花落未央
花落未央 2020-12-20 09:35

DataPackage.SetDataProvider\'s documentation states:

Use the SetDataProvider method when your app ... does not want to supply the data until the tar

2条回答
  •  渐次进展
    2020-12-20 09:48

    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("

    Load html string.

    "); }

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

提交回复
热议问题