简体   繁体   中英

Close() Method Not closing Window when called WPF C#

I'm trying to close a window in WPF, however when I call Close(); it isn't doing anything.

This is the method in my main class MainWindow where I call Close();

public void UserVerified(bool verified)    {
    if (verified == true)        {
        Console.WriteLine("closing...");
        Close();
    }
}

I have a method in another class, which I pass this method too:

if (answer.detail == null)    {
  verify = true;                     
}
else    {
  verify = false;
}
check.UserVerified(verify); 

I also have a function when Close(); is used not to quit the application, rather push it to the System Tray, which is:

protected override void OnClosing(CancelEventArgs e)
{
    e.Cancel = true;
    Hide();
    base.OnClosing(e);
}  

Close(); works in other methods in the MainWindow class however it seems when I run a MainWindow method in another class' method it doesn't work.

It should be noted that it is entering the if (verified == true) {...} statement as the console line is displayed.

Try calling Application.Current.Shutdown() instead of Close() .

Also, I believe it is a better practice to use:

if (verified)

instead of:

if (verified == true)

Same for using:

verify = answer.detail == null;

instead of:

if (answer.detail == null)    {
  verify = true;                     
}
else    {
  verify = false;
}

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