我想从用户控件视图模型获取一个引用来保存用户控件的窗口。我怎么能这样做?
如果我可以从视图模型中获取对视图的引用,那么我可以使用:
Window parentWindow = Window.GetWindow(userControlReference);
所以我的问题是:
答案 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.