gpt4 book ai didi

C# OpenFileDialog 线程启动但未显示对话框

转载 作者:行者123 更新时间:2023-11-30 14:27:32 27 4
gpt4 key购买 nike

我正在尝试完成我的静态 Prompt 类,以便能够从任何地方调用它。但问题是无法使对话框显示。我已经在使用 [STAThread],这是我的代码。

public static string ShowFileDialog()
{
string selectedPath = "";
var t = new Thread((ThreadStart)(() =>
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.RootFolder = System.Environment.SpecialFolder.MyComputer;
fbd.ShowNewFolderButton = true;
if (fbd.ShowDialog() == DialogResult.OK)
{
selectedPath = fbd.SelectedPath;
}
}));
t.SetApartmentState(ApartmentState.STA);
t.Start();

t.Join();
return selectedPath;
}

public static class Prompt 是我的提示类。我从 public partial class Dashboard : Form class

调用它

感谢您的帮助。

最佳答案

当你没有得到异常时,它肯定工作得很好。但是,是的,您看不到对话框的可能性很大。非常难看的问题,你也没有任务栏按钮。找回它的唯一方法是最小化桌面上的其他窗口。

一个对话框,任何对话框,都必须有一个所有者窗口。您应该将该所有者传递给 ShowDialog(owner) 方法重载。如果你不指定它自己去寻找所有者。底层调用是 GetActiveWindow()。想不出什么,桌面窗口现在成为所有者。这不足以确保对话窗口在前面。

至少您必须创建所有者窗口,您现在至少要有任务栏按钮。像这样:

    using (var owner = new Form() { Width = 0, Height = 0,
StartPosition = FormStartPosition.CenterScreen,
Text = "Browse for Folder"}) {
owner.Show();
owner.BringToFront();
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.RootFolder = System.Environment.SpecialFolder.MyComputer;
fbd.ShowNewFolderButton = true;
if (fbd.ShowDialog(owner) == DialogResult.OK) {
selectedPath = fbd.SelectedPath;
}
}

仍然不能保证对话框可见,当用户正在与另一个窗口交互时,您不能将一个窗口推到用户的脸上。但至少有一个任务栏按钮。

我会非常犹豫地展示这个 hack,不要使用它:

    owner.Show();
var pid = System.Diagnostics.Process.GetCurrentProcess().Id;
Microsoft.VisualBasic.Interaction.AppActivate(pid);

吸引用户注意力并让他与您的 UI 交互的正确方法是 NotifyIcon.ShowBalloonTip()。

关于C# OpenFileDialog 线程启动但未显示对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32434984/

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