C#, WPF - OpenFileDialog does not appear

前端 未结 7 669
温柔的废话
温柔的废话 2020-12-09 20:54

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 OpenFil

7条回答
  •  鱼传尺愫
    2020-12-09 21:33

    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.

    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.

提交回复
热议问题