WPF 中的 ShowDialog

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17697715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:13:50  来源:igfitidea点击:

ShowDialog in WPF

c#wpf

提问by Mahmoud Samy

When I make two calls for ShowDialog in WPF the first window open normally, after closing itthe second one doesn't appear.

当我在 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();
}

WindowA:

窗口A:

<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();
    }
}

WindowB:

窗口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();
    }
}

回答by dkozl

In WPF you can specify when application shuts down and by default Application.ShutdownModeis OnLastWindowClosewhich means that when last Windowis closed applications shuts down and in your case first Windowis also last. When you open and close first Windowyour application shuts down and that's why you don't see second Window. You would need to change ShutdownModeto OnExplicitShutdown

在WPF应用程序时关闭,默认情况下,您可以指定Application.ShutdownModeOnLastWindowClose这意味着当最后一个Window是封闭的应用程序会关闭和你的情况首先Window也是最后一次。当您首先打开和关闭Window您的应用程序时,您的应用程序将关闭,这就是您看不到 second 的原因Window。您需要更改ShutdownModeOnExplicitShutdown

<Application ... ShutdownMode="OnExplicitShutdown"/>

but this means that even when you close all your windows application is still running so you have to explicitly call Application.Shutdown()to shutdown your application, for example when main window is closed.

但这意味着即使您关闭所有 Windows 应用程序仍在运行,因此您必须明确调用Application.Shutdown()关闭您的应用程序,例如当主窗口关闭时。

回答by Vasanth Sriram

ShowDialog() function invokes the window modally. Which means code after windowA.ShowDialog(); will not execute until that window is closed.

ShowDialog() 函数以模态方式调用窗口。这意味着 windowA.ShowDialog(); 之后的代码 在该窗口关闭之前不会执行。