当我运行.ShowDialog()时,它将运行并很好地打开,只不过它将在相同的表单/选项卡中打开,而不是一个新的。当我单击任务栏中的Application以简单地打开表单时,就会产生问题,而OpenFileDialog隐藏在应用程序和其他窗口之后,从而导致应用程序基本上被冻结。
唯一的解决办法是慢慢关闭所有其他应用程序(最小化),然后单击OpenFileDialog并继续操作。
string proxyFile = "";
Thread thread = new Thread(() =>
{
OpenFileDialog _ofd = new OpenFileDialog();
_ofd.Filter = "txt|*.txt";
using (OpenFileDialog ofd = _ofd)
if (ofd.ShowDialog() == DialogResult.OK && ofd.CheckFileExists)
{
proxyFile = ofd.FileName;
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join(); // Freeze until dialog is done, then it exits the thread and continues with the filepath.
MessageBox.Show(proxyFile);我必须在线程下运行它,因为它不会在STA中执行(使用CEFSharp JSCallback执行),所以我必须使用线程作为解决办法。
发布于 2017-11-05 07:29:08
最后,我的修正是使用来自"ActiveForm“的方法调用,而在ofd.ShowDialog()方法中,它有一个参数来添加表单/句柄,所以我将它更改为ofd.ShowDialog(ActiveForm),它工作得很好。
https://stackoverflow.com/questions/46964005
复制相似问题