2

我想从用户控件视图模型中获取对包含用户控件的窗口的引用。我怎样才能做到这一点?

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

Window parentWindow = Window.GetWindow(userControlReference); 

所以我的问题是:

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

1 回答 1

4

视图模型通常会继承自IViewAware假设它是一个IScreen实现

您可以将 Screen 投射到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.

于 2012-10-16T09:06:25.860 回答