如何获得使用微卡利从用户控制窗口的引用(How to get a reference to wind

2019-09-24 04:02发布

我想,以保存用户控件的窗口从用户控制视图模型的引用。 我怎样才能做到这一点?

如果我能得到从视图模型视图的参考,然后我可以使用:

Window parentWindow = Window.GetWindow(userControlReference); 

所以我的问题是:

  1. 什么是得到用户控制视图模型,以保存用户控件的窗口中参考的最佳方式?
  2. 如果我想用上面的代码,是什么让一个引用从视图模型在卡利微观上的最佳方式?

Answer 1:

一个ViewModel一般会继承IViewAware假定它是一个IScreen实施

您可以只投一个屏幕IViewAware和使用GetView()方法来获得视图的参考。 我想你可以实现IViewAware在自己的ViewModels和卡利将自动调高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.



文章来源: How to get a reference to window from a user control using Caliburn Micro