3

WPF で ShowDialog を 2 回呼び出すと、最初のウィンドウが正常に開き、閉じた後、2 番目のウィンドウが表示されません。

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

2 に答える 2

5

WPFでは、アプリケーションがいつシャットダウンするかを指定できます。デフォルトApplication.ShutdownModeではOnLastWindowClose、最後Windowに閉じられたときにアプリケーションがシャットダウンされ、あなたの場合は最初Windowも最後になります。最初に開いて閉じるとWindow、アプリケーションがシャットダウンするため、 second が表示されませんWindowShutdownModeに変更する必要がありますOnExplicitShutdown

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

ただし、これは、すべての Windows アプリケーションを閉じてもまだ実行されているためApplication.Shutdown()、たとえばメイン ウィンドウを閉じたときにアプリケーションをシャットダウンするために明示的に呼び出す必要があることを意味します。

于 2013-07-17T11:01:43.827 に答える
4

ShowDialog() 関数はウィンドウをモーダルに呼び出します。これは、windowA.ShowDialog(); の後のコードを意味します。そのウィンドウが閉じられるまで実行されません。

于 2013-07-17T10:48:25.623 に答える