C#WPF应用程序启动错误

时间:2017-12-21 14:55:13

标签: c# wpf xaml

我正在尝试在我的应用程序中创建类似登录窗口的内容。当然,我知道登录窗口不应该启动主窗口 - 这就是我更改App.xamlApp.xaml.cs的原因:

<Application x:Class="WpfApp2.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:WpfApp2"
         >
<Application.Resources>

</Application.Resources>

我在App.xaml.cs中重写了这个事件:

protected override void OnStartup(StartupEventArgs e)
    {
        try
        {
            LoginForm loginForm = new LoginForm();
            MainWindow mainWindow = new MainWindow();
            bool result = (bool)loginForm .ShowDialog();
            if(result)
            {
                mainWindow.Show();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

该代码正在运行,但对我来说并不好,因为我希望将一些参数推送到MainWindow构造函数中。所以,如果我将代码更改为:

protected override void OnStartup(StartupEventArgs e)
    {
        try
        {
            LoginForm loginForm = new LoginForm();
            bool result = (bool)loginForm .ShowDialog();
            if(result)
            {
                MessageBox.Show("I am here");
                MainWindow mainWindow = new MainWindow();
                mainWindow.Show();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

如果我运行此代码 - 我会看到我的登录表单,按下OK按钮后我会看到“我在这里”,之后就会出现异常。请帮帮我。如何解决这个问题?如果没有代码 - 我会添加它

我的登录窗口看起来像(Xaml和Xaml.cs):

<Window x:Class="WpfApp2.LoginForm"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp2"
    mc:Ignorable="d"
    Title="Hello" Width="480" Height="370">
    <Grid>
    <Button Content="Click Me" Width="100" Height="100" Click="SetDialogResultOK">
    </Grid>
<Window.Resources>
</Window.Resources>
</Window>


 public partial class LoginForm : Window
 {
     public LoginForm()
     {
         InitializeComponent();
     }

     private void SetDialogResultOK(object sender, RoutedEventArgs e)
     {
         DialogResult = true;
     }
 }

我的主窗口不包含任何有趣的内容。这是一个简单的窗口

1 个答案:

答案 0 :(得分:2)

您可以将ShutdownMode属性设置为ShutdownMode.OnExplicitShutdown,然后在MainWindow关闭时关闭应用程序:

protected override void OnStartup(StartupEventArgs e)
{
    Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
    try
    {
        LoginForm loginForm = new LoginForm();
        bool result = (bool)loginForm.ShowDialog();
        if (result)
        {
            MessageBox.Show("I am here");
            MainWindow mainWindow = new MainWindow();
            mainWindow.Closed += (ss, ee) => App.Current.Shutdown();
            mainWindow.Show();
        }
        else
        {
            App.Current.Shutdown();
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}