WPF DragDrop:Window.ShowModal()挂起Windows资源管理器 [英] WPF DragDrop: Window.ShowModal() hangs Windows Explorer
问题描述
我已经编写了允许将文件从Windows资源管理器拖放到WPF应用程序中的代码.在放置事件处理程序中,我启动一个窗口,该窗口显示有关放置文件的信息.如果我使用Window.ShowModally()创建此窗口,则Windows资源管理器将挂起/冻结,直到关闭我应用程序中的窗口.但是,如果我使用Window.Show()创建窗口,则不会发生此问题.不幸的是,我要求该窗口以模态显示.
I have written code to allow dropping a file from Windows Explorer into a WPF application. In my drop event handler I launch a window which displays information about the dropped file. If I create this window using Window.ShowModally() then Windows Explorer will hang/freeze until the window in my app is closed. However, if I create the window using Window.Show() this problem does not occur. Unfortunately, I require this window to be shown modally.
大概Windows Explorer线程已锁定,因为它检测到正在使用其文件之一.有没有一种方法可以通知Windows资源管理器它不需要等待应用程序中的窗口关闭?我尝试将DragDropEventArgs.Handled设置为true,但这不能解决问题.
Presumably the Windows Explorer thread is locked because it detects that one of its files is being used. Is there a way to inform Windows Explorer that it does not need to wait for the window in my application to close? I have tried setting the DragDropEventArgs.Handled to true but this does not resolve the problem.
从数据中提取数据后,我不再需要DragDrop,因此,如果可以在Drop事件处理程序中取消或结束DragDrop,那么这将是一个可接受的解决方案.
I no longer require the DragDrop once I have extracted the data from it so if there is a way to cancel or end the DragDrop in my Drop event handler then this would be an acceptable solution.
推荐答案
您不能阻止任何拖放事件处理程序,因为这会挂起D + D管道,并且预期的结果将是死信源窗口.
You cannot block in any of your drag+drop event handlers, that will hang the D+D plumbing and a dead source window is the expected outcome.
有一个简单的解决方法,只需在事件完成后要求调度员运行您的代码以后即可.优雅地使用BeginInvoke()方法.大概是:
There's a simple workaround, just ask the dispatcher to run your code later, after the event completes. Elegantly done with its BeginInvoke() method. Roughly:
private void Window_Drop(object sender, DragEventArgs e) {
// Do something with dropped object
//...
this.Dispatcher.BeginInvoke(new Action(() => {
var dlg = new DialogWindow();
dlg.Owner = this;
var result = dlg.ShowDialog();
// etc..
}));
}
这篇关于WPF DragDrop:Window.ShowModal()挂起Windows资源管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!