C#, WPF - OpenFileDialog does not appear

前端 未结 7 627
温柔的废话
温柔的废话 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:44

    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.

    My solution was to put the open/save code after I created my main window but before I called the Show() method.

提交回复
热议问题