简体   繁体   English

C#WPF应用程序启动错误

[英]C# WPF Application startup error

I am trying to make something like login window in my application. 我正在尝试在应用程序中制作类似登录窗口的内容。 Of course, I understand that login window shouldn't start the main window - that is why I changed the App.xaml and App.xaml.cs like this: 当然,我知道登录窗口不应启动主窗口-这就是为什么我这样更改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>

and I overrided the event in App.xaml.cs : 我在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;
        }
    }

That code is working, but it is not good for me because futher I want to push some parameters into the MainWindow constructor. 该代码可以正常工作,但对我来说却不好,因为我还想将一些参数推入MainWindow构造函数中。 So, if I will change code to this: 因此,如果我将代码更改为此:

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

If I run this code - I will see my login form, after pressing OK button I will see "I am here" and after that it falls to exception. 如果我运行此代码-我将看到我的登录表单,在按“确定”按钮后,我将看到“我在这里”,然后它就变成了异常。 Help me, please. 请帮帮我。 How to solve that problem? 如何解决这个问题? If there is not enought code - I will add it 如果没有足够的代码,我会添加它

My login window looks like (Xaml and Xaml.cs): 我的登录窗口如下所示(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;
     }
 }

My main window does not contain any interesting. 我的主窗口没有任何有趣的内容。 this is a simple window for now 这是一个简单的窗口

You could set the ShutdownMode property to ShutdownMode.OnExplicitShutdown and then shut down the application when the MainWindow is closed: 您可以将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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM