How to get a reference to window from a user control using Caliburn Micro

北慕城南 提交于 2019-12-11 01:35:41

问题


I want to get a reference from a user control view model to the window that holds the user control. How can I do this?

If I can get a reference to the view from view model then I can use:

Window parentWindow = Window.GetWindow(userControlReference); 

So my questions are:

  1. What's the best way to get the reference from user control view model to the window that holds the user control?
  2. If I want to use the above code, what is the best way to get a reference to view from view model in Caliburn Micro?

回答1:


A viewmodel will generally inherit from IViewAware assuming that it's an IScreen implementation

You can just cast a Screen to IViewAware and use the GetView() method to get a reference to the view. I think you can implement IViewAware on your own viewmodels and Caliburn will automatically raise the ViewAttached event so you can keep a reference to the view but I might have a look at the docs

Bottom line: check out the IViewAware interface

Edit:

From the docs...

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.



来源:https://stackoverflow.com/questions/12911074/how-to-get-a-reference-to-window-from-a-user-control-using-caliburn-micro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!