如何使用Caliburn Micro从用户控件获取对窗口的引用 [英] How to get a reference to window from a user control using Caliburn Micro
问题描述
我想从用户控件视图模型获取保存用户控件的窗口的引用。我该怎么做?
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);
所以我的问题是:
- 从用户控件视图模型到保存用户控件的窗口的引用的最佳方法是什么?
- 如果我想使用上面的代码,什么是在Caliburn Micro中从视图模型中获取视图的最佳方法是什么?
推荐答案
A假设它是 IScreen
实现,则viewmodel通常会从 IViewAware
继承
A viewmodel will generally inherit from IViewAware
assuming that it's an IScreen
implementation
您可以将屏幕投射到 IViewAware
并使用 GetView()
方法获取对视图。我认为您可以在自己的视图模型上实现 IViewAware
,Caliburn会自动引发 ViewAttached
事件,以便您保留引用到视图,但我可能会看一下文档
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
底线:查看IViewAware界面
Bottom line: check out the IViewAware interface
编辑:
从文档中...
IViewAware –由类实现需要了解它们所绑定的观点。它具有一个AttachView方法,当将视图绑定到实例时,框架会调用该方法。它具有GetView方法,框架会在为实例创建视图之前调用该方法。这样可以缓存复杂的视图,甚至可以缓存复杂的视图解析逻辑。最后,它有一个事件,当将视图附加到名为ViewAttached的实例时应引发该事件。
这篇关于如何使用Caliburn Micro从用户控件获取对窗口的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!