gpt4 book ai didi

c# - 拖放时吞下异常

转载 作者:太空狗 更新时间:2023-10-29 20:06:35 24 4
gpt4 key购买 nike

我有一个 WinForms 应用程序,我在其中进行 2 个 TreeView 之间的拖放操作。

在某些时候,我想拒绝底层业务实现中的操作,所以我抛出一个异常。我可以在“输出”窗口中看到异常,但问题是我在 UI 中看不到它并且它没有崩溃。

异常去哪儿了?

下面是一些描述问题的代码:

private TreeView tvLeft;
private TreeView tvRight;
private Dictionary<string, int> dico = new Dictionary<string, int>();

void tvLeft_DragDrop(object sender, DragEventArgs e) {

if (e.Data.GetDataPresent(typeof(TreeNode))) {

var tnSource = (TreeNode) e.Data.GetData(typeof(TreeNode));
var tnDestination = tvLeft.GetNodeAt(tvLeft.PointToClient(new Point(e.X, e.Y)));

// if I drag-drop the same node twice, there sould be an Exception
// since the key is already in the dictionary...
// ...but I get no Exception in the UI, the Application.ThreadException
// or Appomain.CurrentDomain.UnhandledException handlers
dico.Add(tnSource.Name, (new Random()).Next());

}

}

最佳答案

我在网上找到了这个解释:

Even with drag-and-drop within the same application, the drag-and-drop is handled through the standard OLE drag-drop mechanism. From OLE's point of view it's dealing with two applications, the source and the target and decouples them appropriately. Since OLE has been around far longer than .NET, OLE has no concept of a .NET exception and therefore can't communicate an exception from the target back to the source. Even if it could, why should the source care that the target couldn't perform the drop?If you want to handle an exception during a DragDrop event you must handle it within your DragDrop event handler, it won't propagate beyond that event handler because there is a managed to unmanaged to managed code transition between the source and the target.

参见 here问题后的第一个答案。

关于c# - 拖放时吞下异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/969983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com