简体   繁体   English

在WPF中,应用程序MainWindow为空(使用Caliburn Micro)

[英]Application MainWindow is null in WPF (using Caliburn Micro)

I am developing a WPF application and I need to get a point to the main window of application inside a control. 我正在开发WPF应用程序,我需要指向控件内应用程序的主窗口。 I am using Caliburn Micro. 我正在使用Caliburn Micro。

Application.Current.MainWindow is null Application.Current.MainWindownull

How can I get a reference to the MainWindow of application in Caliburn Micro? 如何在Caliburn Micro中获得对应用程序MainWindow的引用?

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: 很好笑,我刚刚在另一篇文章中对此进行了回答...尝试在MainWindow.xaml.cs文件的Loaded事件中设置Application.Current.MainWindow属性:

private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    Application.Current.MainWindow = this;
}

1. Understanding of Application.Current.MainWindow 1.了解Application.Current.MainWindow

When your application opens first window ( MainWindow.xaml ), then the window is set to Application.Current.MainWindow . 当您的应用程序打开第一个窗口( MainWindow.xaml )时,该窗口将设置为Application.Current.MainWindow When the window is closed, then another currently opened window is set to Application.Current.MainWindow . 关闭窗口后,另一个当前打开的窗口将设置为Application.Current.MainWindow If the there are no opened windows, then Application.Current.MainWindow is set to null. 如果没有打开的窗口,则将Application.Current.MainWindow设置为null。

eg if you open LoginWindow at startup then Application.Current.MainWindow will be LoginWindow . 例如,如果在启动时打开LoginWindow ,则Application.Current.MainWindow将为LoginWindow When you close LoginWindow , then Application.Current.MainWindow can be Window1 for instance. 当您关闭LoginWindowApplication.Current.MainWindow可以是例如Window1

2. Accessing MainWindow instance 2.访问MainWindow实例

if you want to access instance of MainWindow class you should do following: Application.Current.Windows.OfType<MainWindow>().FirstOrDefault(); 如果要访问MainWindow类的实例,则应执行以下操作: Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();

however, if MainWindow is not opened, then it will return null. 但是,如果未打开MainWindow,则它将返回null。 Don't try to workaround this - if MainWindow is not opened yet, or it is closed already, you should not access it. 不要尝试解决此问题-如果MainWindow尚未打开,或者已经关闭,则不应访问它。

3. MVVM 3. MVVM

in MVVM pattern, you should not access views directly from your viewmodels. 在MVVM模式中,您不应直接从视图模型访问视图。 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. 如果这样做了,您将打破主要的MVVM问题,例如关注点分离,可测试性等。然后的问题是,为什么要使用mvvm。

If you want to perform some action in MainWindow , you should perform the action on MainWindowViewModel . 如果你想在执行某些操作MainWindow ,你应该执行动作MainWindowViewModel If window is opened, it will reflect the changes in ViewModel. 如果打开窗口,它将反映ViewModel中的更改。 If it is not, then the changed does not have to be reflected. 如果不是,则不必反映所做的更改。 MainWindowViewModel should not have direct reference to the MainWindow instance. MainWindowViewModel不应直接引用MainWindow实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM