【问题标题】:What could possibly cause COMExceptions while dragging file over the Desktop?在桌面上拖动文件时可能导致 COMExceptions 的原因是什么?
【发布时间】:2016-03-09 15:16:11
【问题描述】:

它不会崩溃,我在这里提到的所有异常只能在 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 问题看起来很像我的,但似乎有另一个原因和解决方案。

【问题讨论】:

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


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多