C# WPF中的ShowDialog
当我在WPF中两次调用ShowDialog时,第一个窗口正常打开,关闭后第二个窗口不出现C# WPF中的ShowDialog,c#,wpf,C#,Wpf,当我在WPF中两次调用ShowDialog时,第一个窗口正常打开,关闭后第二个窗口不出现 <Application x:Class="Test.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Startup="App_OnStartup"> <
<Application
x:Class="Test.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="App_OnStartup">
</Application>
private void App_OnStartup(object sender, StartupEventArgs e)
{
var windowA = new WindowA();
windowA.ShowDialog();
var windowB = new WindowB();
windowB.ShowDialog();
}
私有void应用程序启动(对象发送方、StartupEventArgs e)
{
var windowA=新的windowA();
windowA.ShowDialog();
var windowB=新的windowB();
windowB.ShowDialog();
}
WindowA:
<Window x:Class="Test.WindowA"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WindowA" Height="129.452" Width="313.356">
<Grid>
<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="139,54,0,0"/>
</Grid>
</Window>
public partial class WindowA : Window
{
public WindowA()
{
InitializeComponent();
}
}
公共部分类WindowA:Window
{
公共窗口a()
{
初始化组件();
}
}
窗口B:
<Window x:Class="Test.WindowB"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WindowB" Height="221.918" Width="300">
<Grid>
<RadioButton Content="RadioButton" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="124,63,0,0"/>
</Grid>
</Window>
public partial class WindowB : Window
{
public WindowB()
{
InitializeComponent();
}
}
公共部分类WindowB:Window
{
公共窗口b()
{
初始化组件();
}
}
ShowDialog()函数以模式调用窗口。这意味着windowA.ShowDialog()之后的代码;在该窗口关闭之前不会执行。在WPF中,您可以指定应用程序关闭的时间,默认情况下为OnLastWindowClose
,这意味着当最后一个窗口关闭时,应用程序关闭,在您的情况下,第一个窗口也是最后一个。当您打开并关闭第一个窗口时,您的应用程序将关闭,这就是为什么您看不到第二个窗口的原因。您需要将ShutdownMode
更改为OnExplicitShutdown
<Application ... ShutdownMode="OnExplicitShutdown"/>
关闭应用程序,例如当主窗口关闭时 第二个在什么时候没有出现?即使在WindowA关闭之后?一次只能显示一个模式/对话框。第二个模式/对话框在关闭第一个模式/对话框后不会出现。确定完成后,您可以再次检查。第二个模式/对话框在关闭第一个模式/对话框后不会出现。您可以解释更多吗?@MSA哪个部分需要澄清?这是应用程序生命周期的一部分吗,从哪里知道这些模式?@MSA更新了我的答案,并做了更多解释。希望如此helps@MSA我的回答中有一个链接解释了ShutdownMode
的工作原理,它是一个带有所有可能选项的链接ShutdownMode
类型