我需要顯示一個 WPF 視窗,并且在用戶關閉它后,一個OpenFileDialog. 這Window1是一個只有一個按鈕和Close()方法的視窗。如果之前打開了 WPF 視窗,OpenFileDialog則不會打開。如何解決?
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
}
}
uj5u.com熱心網友回復:
我不知道我對你的理解有多深。試試這兩個選項:
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
}
}
也許我誤解了你,問題是你的 ShutdownMode 屬性的值設定不正確。
如果是這樣,那么這段代碼:
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();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/513336.html
標籤:C#wpf打开文件对话框
下一篇:隱藏一個帶有觸發器的控制元件
