簡體   English   中英

WPF中的ShowDialog

[英]ShowDialog in 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>

private void App_OnStartup(object sender, StartupEventArgs e)
{
    var windowA = new WindowA();
    windowA.ShowDialog();

    var windowB = new WindowB();
    windowB.ShowDialog();
}

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

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

ShowDialog()函數以模態調用窗口。 這意味着windowA.ShowDialog();之后的代碼 直到該窗口關閉,才會執行。

在WPF中,您可以指定應用程序何時關閉,默認情況下Application.ShutdownModeOnLastWindowClose ,這意味着當最后一個Window關閉時,應用程序關閉,並且您的情況OnLastWindowClose一個Window也是最后一個。 當您打開和關閉第一個Window您的應用程序將關閉,這就是為什么您看不到第二個Window 您需要將ShutdownMode更改為OnExplicitShutdown

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

但這意味着即使關閉所有Windows應用程序仍在運行,因此您必須顯式調用Application.Shutdown()來關閉應用程序,例如,在關閉主窗口時。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM