如何使用Caliburn Micro从用户控件获取对窗口的引用

时间:2012-10-16 09:02:07

标签: wpf mvvm caliburn.micro

我想从用户控件视图模型获取一个引用来保存用户控件的窗口。我怎么能这样做?

如果我可以从视图模型中获取对视图的引用,那么我可以使用:

Window parentWindow = Window.GetWindow(userControlReference); 

所以我的问题是:

  1. 从用户控件视图模型获取引用用户控件的窗口的最佳方法是什么?
  2. 如果我想使用上面的代码,在Caliburn Micro中从视图模型中获取视图参考的最佳方法是什么?

1 个答案:

答案 0 :(得分:4)

视图模型通常会从IViewAware继承,假设它是IScreen实现

您可以将屏幕投射到IViewAware并使用GetView()方法获取对视图的引用。我认为您可以在自己的视图模型上实现IViewAware,Caliburn将自动引发ViewAttached事件,以便您可以保留对视图的引用,但我可能会查看文档

底线:查看IViewAware界面

编辑:

来自文档...

IViewAware – Implemented by classes which need to be made aware of the view that they are bound to. It has an AttachView method which is called by the framework when it binds the view to the instance. It has a GetView method which the framework calls before creating a view for the instance. This enables caching of complex views or even complex view resolution logic. Finally, it has an event which should be raised when a view is attached to the instance called ViewAttached.