C#OpenFileDialog线程启动,但未显示对话框 [英] C# OpenFileDialog Thread start but dialog not shown

查看:388
本文介绍了C#OpenFileDialog线程启动,但未显示对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to finish my static Prompt class to be able to call it from anywhere. But the problem is couldn't make the dialog show. I am already using [STAThread] and here is my code.

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

public static class Prompt is my Prompt Class. I am calling it from public partial class Dashboard : Form class

感谢您的帮助.

推荐答案

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

It surely works just fine when you don't get an exception. But yes, pretty decent odds that you don't see the dialog. Pretty ugly problem, you don't have a taskbar button either. Only way to find it back is by minimizing the other windows on the desktop.

一个对话框,任何对话框,必须有一个所有者窗口.您应该将该所有者传递给ShowDialog(owner)方法重载.如果您未指定,它将自行寻找所有者.底层调用是GetActiveWindow().为了不提出任何要求,桌面窗口现在成为所有者.这不足以确保对话框窗口位于最前面.

A dialog, any dialog, must have an owner window. You are supposed to pass that owner to the ShowDialog(owner) method overload. If you don't specify one that it goes looking for an owner by itself. Underlying call is GetActiveWindow(). To come up with nothing, the desktop window now becomes the owner. That isn't good enough to ensure that the dialog window is in front.

至少必须创建该所有者窗口,现在至少具有任务栏按钮.像这样:

At a minimum you must create that owner window, you'll now at least have the taskbar button. Like this:

    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;
        }
    }

仍然不能保证对话框是可见的,当用户与另一个窗口进行交互时,您不能将一个窗口推入用户的脸部.但是至少有一个任务栏按钮.

Still doesn't guarantee that the dialog is visible, you cannot push a window into the user's face when he's interacting with another window. But at least there's a taskbar button.

我会很犹豫地显示有关此内容的技巧,请不要使用它:

I'll very hesitantly show the hack around that, don't use it:

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

引起用户注意并使用户与您的UI交互的正确方法是NotifyIcon.ShowBalloonTip().

The proper way to draw the user's attention and get him to interact with your UI is NotifyIcon.ShowBalloonTip().

这篇关于C#OpenFileDialog线程启动,但未显示对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆