简体   繁体   English

C#,WPF - OpenFileDialog没有出现

[英]C#, WPF - OpenFileDialog does not appear

I have been searching up and down the web and unfortunately never came across an issue quite like mine, so here goes: 我一直在网上搜索,不幸的是从未遇到过像我一样的问题,所以这里有:

My C# WPF application won't show me no OpenFileDialogs or SafeFileDialogs. 我的C#WPF应用程序不会向我显示OpenFileDialogs或SafeFileDialogs。

private void btnBrowseNet_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.CheckPathExists = true;
        ofd.Multiselect = false;
        ofd.Title = "Open Network Configuration Batch file...";
        ofd.ValidateNames = true;
        ofd.Filter = "Comma Seperated Value Files|*.csv";

        if (ofd.ShowDialog() == true)
        {
           //...
        }
    }

This exact code does in one occasion exactly what it is supposed to do and hardly five minutes later I can click the button all I want, nothing happens but the mouse pointer turning into a little busy-indicator and then nothing. 这个确切的代码在一个场合完全符合它应该做的事情,几乎五分钟之后我可以点击按钮我想要的任何事情都没有发生但是鼠标指针变成了一个忙碌的指示器然后什么都没有。 I can step through the method or do something like this 我可以单步执行该方法或执行此类操作

bool? shown = ofd.ShowDialog();

But no matter what, the dialog won't show. 但无论如何,对话框都不会显示。 Of course, shown will be false in that case. 当然,在这种情况下显示将是错误的。 I wasted one and a half hours searching yesterday and right when I quit I tried it again and all of a sudden it worked. 我昨天浪费了一个半小时的搜索,当我退出时,我再次尝试了它,突然间它起作用了。 Sometimes it works, sometimes it doesn't. 有时候它有效,有时则不然。 But it seems to be project specific because I can paste the same code into a new project and it works like it is supposed to do. 但它似乎是项目特定的,因为我可以将相同的代码粘贴到一个新项目中,它的工作方式就像它应该做的那样。 Also, that's the only thing about the project that seems fishy. 此外,这是该项目唯一似乎有点可疑的事情。 Everything else works as intended. 其他一切都按预期工作。

Has anyone on here ever experienced something similar and thus an idea of what on earth I could do? 有没有人在这里经历过类似的事情,从而了解我到底能做什么? Any help weould be highly appreciated. 任何帮助都应该受到高度赞赏。

There are a large number of possible failure modes for OpenFileDialog. OpenFileDialog存在大量可能的故障模式。 Using one exposes your app to just about any shell extension that's installed on your machine. 使用一个可以将您的应用程序暴露给您计算机上安装的任何外壳扩展。 Many of which can be very destabilizing, it isn't that likely that the extension author has checked if it works properly in a WPF process. 其中许多可能非常不稳定,扩展作者不太可能检查它是否在WPF进程中正常工作。

Tackle this problem by running SysInternals' AutoRuns utility. 通过运行SysInternals的AutoRuns实用程序解决此问题。 Click the Explorer tab and look for the groups that have "ShellEx" in their name. 单击“资源管理器”选项卡,然后查找名称中包含“ShellEx”的组。 Uncheck anything that wasn't published by Microsoft. 取消选中Microsoft未发布的任何内容。 Reboot and check if the problem is solved. 重新启动并检查问题是否已解决。

This happened to me recently. 这最近发生在我身上。 The problem was the Main method wasn't marked as an STAThread which will cause the WPF OpenFileDialog's ShowDialog method to block indefinitely. 问题是Main方法没有标记为STAThread ,这将导致WPF OpenFileDialog的ShowDialog方法无限期地阻塞。

static void Main(string[] args)
{
    var openFileDialog = new OpenFileDialog();
    var result = openFileDialog.ShowDialog();
}

will never exit or throw an exception, whereas 永远不会退出或抛出异常,而

[STAThread]    
static void Main(string[] args)
{
    var openFileDialog = new OpenFileDialog();
    var result = openFileDialog.ShowDialog();
}

will work as expected. 将按预期工作。

I am experiencing a similar problem, and as Garrett suggested, it is an STA issue. 我遇到了类似的问题,正如加勒特所说,这是一个STA问题。 I have been struggling with STA issues a lot in the past few months, as I need to launch screens from a console window (Testing purposes) - this means that the calling thread is not STA, but can be simulated in something like the following: 在过去的几个月里,我一直在努力解决STA问题,因为我需要从控制台窗口启动屏幕(测试目的) - 这意味着调用线程不是STA,但可以模拟如下所示:

    [STAThread]
    private void Execute() {
        try {
            Thread t = new Thread(() => {
                OpenFileDialog dlg = new OpenFileDialog();
                // The following would not return the dialog if the current
                // thread is not STA
                var result = dlg.ShowDialog();
            });

            t.SetApartmentState(ApartmentState.STA);
            t.Start();
        } catch (Exception ex) {
            // Handle the exception
            ex.LogException();
        }
    }

Unforunately, it did not work for me to just mark the method as STAThread, I had to launch the operation in a thread marked as STA. 不幸的是,仅仅将方法标记为STAThread对我不起作用,我必须在标记为STA的线程中启动操作。

I know this question was ask in 2010 and the direct answer wasn't the one I'll provide but as today date there is an other reason to have this problem. 我知道这个问题是在2010年问的,而直接答案不是我提供的那个,但是今天的日期还有其他原因可以解决这个问题。

I recently installed my software on a fresh virtual machine which works perfectly on my dev computer and many other testing machines. 我最近在一台新的虚拟机上安装了我的软件,它在我的开发计算机和许多其他测试机器上运行良好。

The Windows 7 virtual machine was too fresh and did not have the SP1 KB976932 installed. Windows 7虚拟机太新鲜,没有安装SP1 KB976932

Once installed, I could use the open sand save file dialogs. 安装后,我可以使用打开的沙子保存文件对话框。

I hope this will help someone else. 我希望这会帮助别人。

In the console application you will need STAThread appartment for it to work. 在控制台应用程序中,您需要STAThread appartment才能工作。 But WPF is different. 但WPF与众不同。

I would advise you using the File Dialogs only after the window starts and the Main Thread starts working. 我建议您仅在窗口启动并且主线程开始工作后才使用文件对话框。 Try showing your dialog in some MainWindow event of its lifecycle. 尝试在其生命周期的某个MainWindow事件中显示您的对话框。

sometime [staThread] not working, you can try this: 有时[staThread]无法正常工作,你可以试试这个:

    public void showOpenFileDialog()
    {
        OpenFileDialog im = new OpenFileDialog();
        if (im.ShowDialog() == DialogResult.OK)
        {
            textBox1.Text = im.FileName;
        }
    }


    private void select_button_Click(object sender, EventArgs e)
    {
        Thread newThread = new Thread(new ThreadStart(showOpenFileDialog));
        newThread.SetApartmentState(ApartmentState.STA);
        newThread.Start();
    }

Not sure if you figured it out or not, but I recently had this same problem. 不确定你是否弄明白,但最近我遇到了同样的问题。 In my case, the problem was that my application hadn't established an existing window. 在我的情况下,问题是我的应用程序没有建立现有的窗口。

My code looked something like this. 我的代码看起来像这样。

private void Application_Startup(object sender, StartupEventArgs e) {
    string startupFileName = String.Empty();
    if ( startupMode = StartupMode.Load ) {
        // open existing file
        OpenFileDialog openDlg = new OpenFileDialog();
        if (openDlg.ShowDialog() != true)
             return;
        startupFileName = openDlg.FileName;
    } else {
        // create a new file
        SaveFileDialog saveDlg = new SaveFileDialog();
        if (saveDlg.ShowDialog() != true)
             return;
        startupFileName = saveDlg.FileName;
    }

    // show my main application window
    MainWindow myMainWindow = new MainWindow(startupFileName);
    myMainWindow.Show();
}

The OpenFileDialog (or SaveFileDialog) would immediately return false without showing because my application had no window for it to attach itself to. OpenFileDialog(或SaveFileDialog)会立即返回false而不显示,因为我的应用程序没有窗口将它自身附加到。

My solution was to put the open/save code after I created my main window but before I called the Show() method. 我的解决方案是在创建主窗口之后但在调用Show()方法之前放置打开/保存代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM