简体   繁体   中英

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:

<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 :

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. 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):

<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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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