0

I have an app that consists of 2 parts. 1st part is Login form, where user needs to enter login and password. If they are correct, it start "Editor" window where user can work.

For now in order to launch second window I use:

var editorWindow = new EditorWindow();
editorWindow.Activate();

The problem is that Login window is still there, and while it is not critical, I still want to close it after Login is done.

First time I tried to add Window.Close() after opening the 2nd window in the .cs file of 1st Window, so

var editorWindow= new EditorWindow();
editorWindow.Activate();

var oldWindow = new MainWindow();
oldWindow.Close();

Which resulted Attempted to read or write protected memory eror. I tried to do it in the 2nd Window .cs file like this:

this.InitializeComponent();

var oldWindow = new MainWindow();
oldWindow.Close();

Which resulted the same error

So how can I do this properly?

Rev1k
  • 37
  • 6
  • 1
    Just call `this.Close()` right after you've called `Activate()` on the new window? Please post a reproducible example if you need more help. – mm8 Jan 25 '23 at 10:48

1 Answers1

0

If you open the second window in the code-behind of the first window, you should be able to just call this.Close() right after you've called Activate() on the new window:

var editorWindow= new EditorWindow();
editorWindow.Activate();

this.Close();

If you open the EditorWindow from somewhere else, you need to get a reference to the first window to be able to close it. You could for example use a variable in the App class for this as suggested here.

mm8
  • 163,881
  • 10
  • 57
  • 88