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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18660256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:36:20  来源:igfitidea点击:

Application MainWindow is null in WPF (using Caliburn Micro)

c#wpfcaliburn.micro

提问by mans

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.

我正在开发一个 WPF 应用程序,我需要指向控件内的应用程序主窗口。我正在使用 Caliburn Micro。

Application.Current.MainWindowis null

Application.Current.MainWindownull

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

如何MainWindow在 Caliburn Micro 中获得对应用程序的引用?

回答by Sheridan

That's funny, I've just answered this in another post... Try setting the Application.Current.MainWindowproperty in the Loadedevent in the MainWindow.xaml.csfile:

这很有趣,我刚刚在另一篇文章中回答了这个问题......尝试在文件Application.Current.MainWindow中的Loaded事件中设置属性MainWindow.xaml.cs

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

回答by Liero

1. Understanding of Application.Current.MainWindow

一、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.

当您的应用程序打开第一个窗口 ( MainWindow.xaml) 时,该窗口将设置为Application.Current.MainWindow。当窗口关闭时,另一个当前打开的窗口被设置为Application.Current.MainWindow。如果没有打开的窗口,则 Application.Current.MainWindow 设置为 null。

e.g. if you open LoginWindowat startup then Application.Current.MainWindowwill be LoginWindow. When you close LoginWindow, then Application.Current.MainWindowcan be Window1for instance.

例如,如果您LoginWindow在启动时打开,那么Application.Current.MainWindow将是LoginWindow. 例如,当您关闭时LoginWindow,则Application.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. Don't try to workaround this - if MainWindow is not opened yet, or it is closed already, you should not access it.

但是,如果 MainWindow 未打开,则它将返回 null。不要尝试解决此问题 - 如果 MainWindow 尚未打开或已关闭,则不应访问它。

3. MVVM

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.

在 MVVM 模式中,您不应直接从视图模型访问视图。如果你这样做了,你会打破主要的 MVVM 问题,比如关注点分离、可测试性等。 那么问题是,你为什么想要 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. MainWindowViewModelshould not have direct reference to the MainWindowinstance.

如果要在 中执行某些操作MainWindow,则应在 上执行该操作MainWindowViewModel。如果窗口被打开,它将反映 ViewModel 中的变化。如果不是,则不必反映更改的内容。MainWindowViewModel不应直接引用MainWindow实例。