我有2个WPF窗口。App.xaml.cs在显示“状态”时选择“第一个窗口”并读取一些数据,然后将其关闭。然后App.xaml.cs打开第二个窗口。当我调试代码正确执行但关闭第一个窗口后,它将关闭整个应用程序。我究竟做错了什么?App.xaml.cs中不可能吗?
这是代码。(对于该测试,我在后面使用代码而不是MVVM)在此代码中,我放置了一个按钮以关闭第一个窗口。
App.xaml.cs:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
TestWindow tw = new TestWindow();
bool? rslt = tw.ShowDialog();
if (rslt == true)
{
MainWindow mw = new MainWindow();
mw.Show(); //I am not sure why the Application close itself
}
}
}
Run Code Online (Sandbox Code Playgroud)
TestWindow.xaml:
<Window x:Class="Shell.Startup.TestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TestWindow" Height="300" Width="300">
<Grid>
<Button x:Name="ButtonYes" Content="Yes" HorizontalAlignment="Left" Height="21" Margin="95,192,0,0" VerticalAlignment="Top" Width="66" RenderTransformOrigin="1.485,0.81" Click="ButtonYes_Click"/>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
TestWindow.xaml.cs:
public partial class TestWindow : Window
{
public TestWindow() …Run Code Online (Sandbox Code Playgroud)