获取视图& ViewModel从一个插件 [英] Get the View & ViewModel from a plugin
问题描述
在我的应用程序中,我有一个Designer,当我在ViewModels列表中添加任何ViewModel时,其视图将显示在Designer中。
我的问题是:在我的列表中添加我的插件的viewModel时,如何保持此绑定?如何使其视图出现在我的设计师中?
这是一个有趣的问题,有很多方法可以实现。
这取决于你的插件。
如果插件更加集成而不是孤立的话。您应该考虑一些工厂
类,例如命名为 ViewResolver
或 ViewModelResolver
。他们可以使用
名称作为字符串
,一个类型
,一个类型的接口
或按惯例工作。
目的是为视图找到VM,反之亦然。这个服务还应该定位插件VM,或者用MEF导入它们。例如:
要查看更多信息,请参阅viewmodel-first,viewmodel婚姻等。 / p>
或
如果您的插件完全充满了独立运行的组件。我建议您的主机应用程序应该在其视图模型中有一个作为插件的组件列表。所以模型将是一个组件,无论它是否包含视图和其他一切,因为托管应用程序是管理组件。所以你会有一个ObservableCollection绑定到内容控件。 内容控件可以作为一个整体托管该插件。
这些是两种可能的方式,就像我上面提到的那样,没有 mvvm,它总是取决于你的用例...
但我希望这可以指导你正确的方向...
HTH
I have an application which use plugins. Each plugin is developped following MVVM, so I have a View, binded to a View-Model inside. like the following picture:
In my application I have a Designer, when I add any ViewModel in my ViewModels list, its view will appear in the Designer.
My question is: How can I keep this binding when adding the viewModel of my plugin in my list ?? how to make its view appear in my designer ?
This is an interesting question... and like always there are many ways to accomplish it.
It depends what your plugins are.
If the plugin is more integrated and not so isolated. You should think about some Factory
classes which are for example named like ViewResolver
or ViewModelResolver
. They can take parameters like the name as string
, a type
, a type of an interface
or work by conventions.
Purpose is to find the VM for a view and vice versa as a central service. This service should also locate plugin VM or import them for example with MEF.
For more info’s google on view-first, viewmodel-first, view viewmodel marriage, etc.
Or
If your plugins are fully fleshed out components which run independently. I would suggest your host application should have in its viewmodel a list of components which are the plugins. So the Model will be a Component no matter if it contains the view and everything else because of the hosting app is to manage the components. So you would have an ObservableCollection which are bound to content control. The content control can then host the plugin as a whole.
These are two possible ways... like I mentioned above there is no "the" way in mvvm and it always depends on your use cases... But I hope this guides you in the right direction...
HTH
这篇关于获取视图& ViewModel从一个插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!