WPF 中 OpenClipboard 失敗問題 - HRESULT: 0x800401D0 的解決方案


解決方案可以參照:https://stackoverflow.com/questions/12769264/openclipboard-failed-when-copy-pasting-data-from-wpf-datagrid

關鍵的問題在於 Clipboard.SetText() 方法會觸發異常。

 

方案一:

It is a bug in the WPF Clipboard handler. You need to handle the unhandled exception in the Application.DispatcherUnhandledException event.

Add this attribute to the Application element in your App.xaml

DispatcherUnhandledException="Application_DispatcherUnhandledException"

 

Add this code to your App.xaml.cs file

void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
    var comException = e.Exception as System.Runtime.InteropServices.COMException;

    if (comException != null && comException.ErrorCode == -2147221040)
         e.Handled = true;
}

 

方案二:

We are using .NET 4.0. We had the same problem, but after logging off the system, code used to work fine for some time.

Finally we found the alternative.

If you want to copy a string to the clipboard,

string data = "Copy This"

Till now I was using the following method

Clipboard.SetText(data);

It was failing again and again. Then I looked at other methods available to set text in the clipboard in Clipboard Class and tried the following:

Clipboard.SetDataObject(data);

And it worked :). I never had the issue again.

 

更多的方案參照最上方的鏈接。

 

如果是 DataGrid 的復制問題,一般 DataGridTextColumn 是可以直接進行復制的,如果是 DataGridTemplateColumn,則需要添加

ClipboardContentBinding="{Binding 屬性}"

到 DataGridTemplateColumn 中就能解決此問題。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM