使用 MEF 时组合零件的理想场所 + MVVM 轻型工具包?
现在,我正在开发使用 MEF 和 MVVM light 工具包的新应用程序,我遇到了无法加载特定插件的情况,因为它们依赖于未导出的部分。该部分不会被导出,因为它是一个视图,并且该视图尚未通过 MEF 解析。
似乎每次我使用 MEF 时,我都需要将部件组合步骤(即创建容器、目录,然后组合部件)移至离主应用程序越来越近的位置。我相信我现在正处于那个阶段。然而,问题是我不知道到底该把它放在哪里。
我想把它放在 MainWindow 中,我相信它是由 MVVM light takeit 项目模板创建的。我在 MainViewModel 和 Model 上使用了 Export
属性。我在 ViewModelLocator 中导入 ViewModel,然后在 ViewModel 中导入 Model。对我来说,这似乎足以确保一切都正确组合。然而,虽然 ViewModel 是正确组合的(它命中了它的构造函数),但 Model 却是错误的。我在合成过程中检查了输出窗口,没有发现任何 MEF 错误。
总而言之,这些是我的问题: 1. 组合零件的“正确”位置在哪里?在 WPF / MVVM 情况下,它总是位于应用程序的开头吗? 2. 在输出窗口中没有 MEF 错误的情况下,还有哪些其他技术可以找出问题未得到解决的原因?通常,我会逐步完成 Compose 来尝试获得一些见解。还有比这更好的调试技术吗? 3. 你能想到为什么我的模型无法加载吗? 4. 有没有好的MEF + MVVM轻工具包项目可以下载学习?
Now that I'm working on my newer application that uses MEF with the MVVM light toolkit, I've run into a case where I can't load specific plugins because they rely on a part that isn't getting exported. That part isn't getting exported because it's a View, and the View isn't resolved with MEF (yet).
It seems that every time I play with MEF, I need to move the part composition step (i.e. create container, catalog, then compose parts) closer and closer to the main application. I believe that I am now at that point. However, the problem is that I'm not sure exactly where to put it.
I'd like to put it MainWindow, which I believe is created by the MVVM light tookit project template. I have used the Export
attribute on MainViewModel and Model. I am importing the ViewModel in the ViewModelLocator, and I import the Model in the ViewModel. To me, this seems like enough to ensure that everything gets composed properly. However, while the ViewModel is composed properly (it hits its constructor), the Model is not. I checked the output window during composition, and I don't get any MEF errors there.
So in summary, these are my issues:
1. Where is the "right" place to compose parts? In the WPF / MVVM case, would it always be at the start of the app?
2. In the absence of MEF errors in the output window, what are other techniques to figure out why things aren't getting resolved? Normally, I will step all the way through Compose to try to get some insight. Are there better debugging techniques than this?
3. Can you think of why my Model wouldn't get loaded?
4. Are there good MEF + MVVM light toolkit projects I can download to study?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需使用MEFedMVVM,您就不会遇到任何这些问题。顾名思义,它是从头开始构建的,旨在解决您提到的问题类型。它还有很多用于构建 mvvm 应用程序的宝贵功能。祝你好运。
Just use MEFedMVVM and you will not have any of these problems. As the name suggests it has been build from the ground up to solve the type of issues that you are mentioning. And it has a lot more precious features for building mvvm apps. Good luck.