c# - 在桌面上拖动文件时可能导致 COMExceptions 的原因是什么?

标签 c# wpf drag-and-drop drag comexception

它没有崩溃,我在这里提到的所有异常只能在 Visual Studio 的输出窗口中看到。下面是拖动的实现:
WPF:

<StackPanel Orientation="Vertical"
            MouseDown="DragShortcut"
            x:Name="Shortcut">
    <Image Source="{Binding Icon}"/>
    <Label Content="{Binding ShortcutLabel}"/>
</StackPanel>

cs代码:

private void DragShortcut(object sender, MouseButtonEventArgs e)
{
    if (e.LeftButton != MouseButtonState.Pressed)
        return;

    var dataObject = new DataObject(DataFormats.FileDrop, new[] { Options.DragDropOptions.ShortcutPath });
    DragDrop.DoDragDrop(Shortcut, dataObject, DragDropEffects.Copy);
}

一切似乎都按预期工作,但每次我在桌面或资源管理器窗口上拖动某些内容时,我都会在 Visual Studio 的“输出”窗口中收到以下消息:

...
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in PresentationCore.dll
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in PresentationCore.dll
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in PresentationCore.dll
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in PresentationCore.dll
A first chance exception of type 'System.NotImplementedException' occurred in PresentationCore.dll
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in PresentationCore.dll
... 

当 Visual Studio 设置为遇到此类异常时停止时,我可以看到以下异常:

System.Runtime.InteropServices.COMException was unhandled
Message: An exception of type 'System.Runtime.InteropServices.COMException' occurred in PresentationCore.dll and wasn't handled before a managed/native boundary
Additional information: Invalid FORMATETC-Structure (Exception HRESULT: 0x80040064 (DV_E_FORMATETC))

System.NotImplementedException was unhandled
Message: An exception of type 'System.NotImplementedException' occurred in PresentationCore.dll and wasn't handled before a managed/native boundary
Additional information: The method or operation is not implemented.

它不会导致崩溃或其他任何事情,只是让我作为开发人员在后台发生这样的事情感到不舒服。有没有人知道它会是什么?

编辑:
This问题看起来很像我的,但似乎有另一个原因和解决方案。

最佳答案

这是完全正常的。您拖过的另一个进程的任何窗口都会使该进程戳您拖动的对象,以查看它是否支持特定格式或可以将对象转换为另一种格式。完成 COM calls在引擎盖下。如果答案是"is",那么您会看到光标发生变化,表示您可以放下。

WPF 中的 IDataObject 接口(interface)实现通过抛出异常来表示“不”。在 .NET 程序中生成 COM 故障代码的正常方式。该异常由 CLR 转换为 COM 错误代码,即 HRESULT,以告知进程它不会工作。请注意 Exeption 类如何具有 HResult property ,这就是进程看到的。

如果您要求,调试器会尽职尽责地显示“第一次机会”异常通知。右击Output窗口,“Exception Messages”选项,默认开启。实际上没有任何问题,异常被捕获并被优雅地处理。功能,而不是错误。

关于c# - 在桌面上拖动文件时可能导致 COMExceptions 的原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34092581/

相关文章:

c# - 我的类(class)应该订阅自己的公共(public)事件吗?

c# - 如何使用 LINQ 过滤字典并将其返回到相同类型的字典

c# - 我可以通过 DataTrigger (XAML) 调用函数吗?

WPF DataTrigger 找不到触发器目标

java - JTabbedPane 之间的间距

c# - ASP.NET Core 1.1 用户身份模拟

c# - HttpClient 不在 Windows 2008 R2 上发送接受编码

在任务 Pane 中使用时,WPF 组合框不会保持打开状态

javascript - Dojo 拖放 : how to retrieve order of items?

javascript - 如何区分拖放和拖放中止