0

I have a login form which starts a dialog of a lecturerWindow

When swapping windows it looks like that:

//LoginWindow to LecturerClient
 this.Visibility = Visibility.Collapsed;
 LecturerWindow lecturerClient = new LecturerWindow(self);
 lecturerClient.Owner = this;
 lecturerClient.ShowDialog();
 this.Visibility = Visibility.Visible; // so when the lecturerClient dialogs exits - the login form will be visible

And also my LecturerWindow opens another dialog:

//LecturerClient To Session
                Dispatcher.Invoke(() =>
                {
                    Visibility = Visibility.Collapsed;
                    Session newSession = new Session(mySelf, Courses.Find(item => item.courseId == courses[1].ToString()));
                    newSession.Owner = this;
                    newSession.ShowDialog();
                    Visibility = Visibility.Visible;
                });

The issue begins when my Session dialog closes and suddenly both my LoginWindow and my LecturerWindow goes Visible, it's like my LoginWindow thinks the session closing is the lecturerWindow closing

Thank you in advance!

3
  • I can't explain exactly why this is, but Dispatcher.Invoke is what's causing the problem. I was able to reproduce your issue, and removing that line fixes it. Not sure what you're using it for, though, so removing it might not be an option. Commented Mar 26, 2020 at 21:54
  • Well so what is the alternative for controlling ui elements from threads? Commented Mar 27, 2020 at 22:03
  • That depends, which class are you using for multithreading? (i.e. Task, BackroundManager, etc.) Commented Mar 28, 2020 at 3:04

1 Answer 1

0

I'm sure you have solved your issue already, but here's a solution for future readers:

Instead of hiding the windows in the app by setting window.Visibility = Visibility.Collapsed;, I would simply open the new window and then close the old window:

// in LoginWindow.xaml.cs
private void SubmitButton_OnClick(object sender, RoutedEventArgs e)
{
    // create a new window to show
    LecturerWindow newWindow = new LecturerWindow()
    {
        Title                 = "Lecturer Window",
        WindowStartupLocation = WindowStartupLocation.CenterScreen
    };
    // change the app main window to use the new window
    Application.Current.MainWindow = newWindow;
    newWindow.Show();
    // close the LoginWindow
    this.Close();
}

Note that if you close the current window before opening the new one, if there are no other windows open, the application will initiate shutdown. This behavior is determined by the Application.ShutdownMode property. By opening the new window before closing the current we avoid this (like shown above).

The same can be done in your LecturerClient to open the Session window.

When closing the LecturerClient window you can open a new LoginForm window again by subscribing to the Closing event:

// in LecturerWindow.xaml.cs
public LecturerWindow()
{
    InitializeComponent();
    Closing += OnClosing;
}

// Open the login form again when this window is closing
private void OnClosing(object sender, CancelEventArgs e)
{
    // create a new window to show
    LoginWindow newMainWindow = new LoginWindow()
    {
        Title                 = "Login form window",
        WindowStartupLocation = WindowStartupLocation.CenterScreen
    };
    // change the app main window to use the new window
    Application.Current.MainWindow = newMainWindow;
    newMainWindow.Show();
    // This window is already closing, so we don't need to call Close()
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.