【发布时间】:2017-10-26 21:20:11
【问题描述】:
当我运行 .ShowDialog() 时,它会运行并正常打开,但它会以相同的表单/选项卡而不是新的表单/选项卡打开。当我单击任务栏中的应用程序图标以简单地打开表单时,这会导致问题,而 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 执行)所以我必须使用线程作为解决方法。
【问题讨论】:
-
ofd 是需要 STA 的 Windows Shell 的包装器。你是什么意思它没有在 STA 中执行?
标签: c# openfiledialog