获取视图& ViewModel从一个插件 [英] Get the View & ViewModel from a plugin

查看:144
本文介绍了获取视图& ViewModel从一个插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用插件的应用程序。每个插件都是在MVVM下开发的,所以我有一个View,绑定到一个View-Model里面。如下图所示:





在我的应用程序中,我有一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆