Application MainWindow is null in WPF (using Caliburn Micro)
Asked Answered
V

2

9

I am developing a WPF application and I need to get a point to the main window of application inside a control. I am using Caliburn Micro.

Application.Current.MainWindow is null

How can I get a reference to the MainWindow of application in Caliburn Micro?

Valina answered 6/9, 2013 at 14:33 Comment(0)
B
20

That's funny, I've just answered this in another post... Try setting the Application.Current.MainWindow property in the Loaded event in the MainWindow.xaml.cs file:

private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    Application.Current.MainWindow = this;
}
Boxthorn answered 6/9, 2013 at 14:43 Comment(4)
That's funny, I just saw that post a few minutes ago.Hulbard
Just tried this, before I set it I checked and it was set (but set it again), but then later its null again, any idea what could be causing that? As far as I can see my application does not ever set that, so is there a WPF generic scenario that could set it to null?Nitty
Sorry, but I'm not aware of any situation where that would happen... it's never happened to me, but if you're using any 3rd party frameworks, they might do that for some reason.Boxthorn
Application.MainWindow is set automatically to the first window opened in the application. When the window is closed, it is either set to next opened window or to null in case there are no opened windows.Immutable
I
6

1. Understanding of Application.Current.MainWindow

When your application opens first window (MainWindow.xaml), then the window is set to Application.Current.MainWindow. When the window is closed, then another currently opened window is set to Application.Current.MainWindow. If the there are no opened windows, then Application.Current.MainWindow is set to null.

e.g. if you open LoginWindow at startup then Application.Current.MainWindow will be LoginWindow. When you close LoginWindow, then Application.Current.MainWindow can be Window1 for instance.

2. Accessing MainWindow instance

if you want to access instance of MainWindow class you should do following: Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();

however, if MainWindow is not opened, then it will return null. Don't try to workaround this - if MainWindow is not opened yet, or it is closed already, you should not access it.

3. MVVM

in MVVM pattern, you should not access views directly from your viewmodels. If you did, you would break the major MVVM concerns, like Separation of concerns, testability, etc, etc. The question is then, why you want mvvm.

If you want to perform some action in MainWindow, you should perform the action on MainWindowViewModel. If window is opened, it will reflect the changes in ViewModel. If it is not, then the changed does not have to be reflected. MainWindowViewModel should not have direct reference to the MainWindow instance.

Immutable answered 15/7, 2015 at 10:45 Comment(2)
I have very little experience with WPF, but information in second point seems to be imprecise. I don't think "MainWindow class" exists in WPF and, [documentation explicitly says] that first item Application.Windows isn't necessarily the main window: "If MainWindow is subsequently set with a reference to a different Window, the position of the item with the reference to the main window will change, while the order of items in Windows remains the same. Consequently, always use MainWindow to refer to the main window instead of the first item in Windows."Adalbertoadalheid
@TomaszMaczyński: Fixed, thanks! It was just formmating issue. The type argument here OfType<MainWindow>() is the tricky part that was missingImmutable

© 2022 - 2024 — McMap. All rights reserved.