简体   繁体   中英

How to close a startup window

I have C# WPF application that has a login windows as it's startup URI. I want to close the the startup window, which is the login window when the combination of username and password is correct.

I have this code

var username = query.username;
var password = query.password;

if (userTexbox.Text == username.ToString() && passwordbox.Password == password.ToString())
{
    var entity = new Log
    {
        user = userTexbox.Text,
        dateTime = DateTime.Now.ToString()
    };
    logCollection.Insert(entity);

    Mainwindow main = new MainWindow();
    this.Close();
}
else
{
    MessageBox.Show("User not found/Wrong credentials", "Re-enter Credentials");
}

But even with the Close() method, the login window is still visible although not active.

Change the logic: set the main window as startup window, then display the login window from main window. Check the bellow code, it's for the main window's contructor:

...
public MainWindow()
{
    InitializeComponent();

    //--init main windows minimized
    WindowState = System.Windows.WindowState.Minimized;

    Loaded +=
        delegate
        {
            //hide main window
            Hide();

            //initialize the login window
            var loginWin = new LoginWindow
            {
                WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen,
                WindowStyle = System.Windows.WindowStyle.SingleBorderWindow,
            };

            loginWin.Closed += delegate
            {
                //check login result (OK)
                if (loginWin.Result != LoginResult.Success)
                    Application.Current.Shutdown(1);

                //--display the main window
                Show();
                WindowState = System.Windows.WindowState.Normal;
                Focus();
            };

            //show&focus the login
            loginWin.Show();
            loginWin.Focus();
        };
}
...

I hope it helps.

EDIT

Here the LoginWindow code behind.

public partial class LoginWindow : Window
{
    public LoginResult Result { get; private set; }

    public LoginWindow()
    {
        InitializeComponent();
    }

    private void ButtonLogin_OnClick(object sender, RoutedEventArgs e)
    {
        //example login button
        Result = DoLogin();
        Close();
    }

    private LoginResult DoLogin()
    {
        //NOTE: Add your login logic here (for now sucess response)
        return LoginResult.Success;
    }
}

public enum LoginResult
{
    Unknow,
    Success,
    Failed
}

What you are missing here is Assignment of MainWindow

Application.Current.MainWindow = main;

After creating new instance of MainWindow if you assign it the Application.Current.MainWindow (before closing the login window) then it has a main window to continue the STA thread and the LoginWindow thread can be released.

Do let me know if this helps.

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