Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF中的ShowDialog_C#_Wpf - Fatal编程技术网

C# 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"> <

当我在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>

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
类型