I need to show a WPF window and, after a user closes it, an OpenFileDialog
. The Window1
is a window that has only one button with a Close()
method.
The OpenFileDialog
does not open if a WPF window has been opened before. How to fix it?
public partial class App : Application
{
void ApplicationOnStartup(object sender, StartupEventArgs args)
{
// If I remove comments and window opens, the OpenFileDialog will not
// Window1 window1 = new();
// window1.ShowDialog();
OpenFileDialog folderBrowser = new();
bool? test = folderBrowser.ShowDialog(); // true/false if the dialog was opened; false if window was opened before
}
}
CodePudding user response:
I don't know how well I understood you. Try these two options:
public partial class App : Application
{
void ApplicationOnStartup(object sender, StartupEventArgs args)
{
MainWindow = new Window1();
MainWindow.Show();
OpenFileDialog folderBrowser = new();
bool? test = folderBrowser.ShowDialog();
}
}
public partial class App : Application
{
void ApplicationOnStartup(object sender, StartupEventArgs args)
{
Window window1 = new();
window1.Loaded = OpenFileDialogAfterLoadedWindow;
window1.ShowDialog();
}
private static void OpenFileDialogAfterLoadedWindow(object sender, RoutedEventArgs e)
{
// First, turn off the wiretap so that there were no repeated calls (this is rare, but possible).
Window window = (Window)sender;
window.Loaded -= OpenFileDialogAfterLoadedWindow;
OpenFileDialog folderBrowser = new();
bool? test = folderBrowser.ShowDialog();
// Some Code
}
}
Perhaps I misunderstood you, and the problem is that you have the value of the ShutdownMode property set incorrectly.
If so, then this code:
public partial class App : Application
{
void ApplicationOnStartup(object sender, StartupEventArgs args)
{
ShutdownMode = ShutdownMode.OnExplicitShutdown;
Window1 window1 = new();
window1.ShowDialog();
OpenFileDialog folderBrowser = new();
bool? test = folderBrowser.ShowDialog(); // true/false if the dialog was opened; false if window was opened before
// You need to explicitly close the application.
Shutdown();
// Or create a window and change the application close mode.
MainWindow = new SomeWindow();
ShutdownMode = ShutdownMode.OnMainWindowClose;
MainWindow.Show();
}
}