出于沮丧和超过 3 天的谷歌搜索这个问题......我别无选择,只能用我的问题来打扰你们。
我正在创建一个 Silverlight 应用程序。我正在使用 MEF。当我尝试运行我的应用程序时,出现以下错误。
The invocation of the constructor on type 'IFG.Silverlight.Client.Views.MenuView' that matches the specified binding constraints threw an exception. [Line: 25 Position: 47]
其内部异常如下...
The composition remains unchanged. The changes were rejected because of the following error(s): The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.
1) No valid exports were found that match the constraint '((exportDefinition.ContractName == "MenuViewModel") AndAlso (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity") AndAlso "IFG.Silverlight.Client.ViewModel.MenuViewModel".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))', invalid exports may have been rejected.
Resulting in: Cannot set import 'IFG.Silverlight.Client.Views.MenuView.ViewModel (ContractName="MenuViewModel")' on part 'IFG.Silverlight.Client.Views.MenuView'. Element: IFG.Silverlight.Client.Views.MenuView.ViewModel (ContractName="MenuViewModel") --> IFG.Silverlight.Client.Views.MenuView
行。我的代码非常简单,因为它是一个测试应用程序。
我有一个接口(interface) IRetailModel
namespace IFG.Silverlight.Client.Common
{
public interface IRetailModel
{ ............
然后我有一个实现这个接口(interface)的类
namespace IFG.Silverlight.Client.Model
{
[Export(typeof(IRetailModel))]
public class RetailModel : IRetailModel
{ .................
然后我有我的 ViewModel View
namespace IFG.Silverlight.Client.ViewModel
{
[PartCreationPolicy(CreationPolicy.NonShared)]
[Export(ViewModelTypes.MenuViewModel)]
public class MenuViewModel : IFGViewModelBase
{
IRetailModel _model;
[ImportingConstructor]
public MenuViewModel(IRetailModel model)
{
好吧,我发现有一个名为 MefX 的神奇工具,它可以深入调试您的代码并告诉您发生了什么……我无法让它工作。
我按照这篇文章的指示 http://blogs.msdn.com/b/nblumhardt/archive/2009/09/24/debug-composition-from-within-visual-studio.aspx
当我尝试运行它说
Error: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
然后回到谷歌,我发现了这个 Visual MefX(它是相同的,但有一个 GUI),我可以加载 .xap 但基本上它给了我从 Visual Studio 获得的相同信息。我找不到该死的[因为]...
我真的,诚实而深刻地,对这种情况感到沮丧。谁能向我解释我在哪里没有让 MefX 完成它的工作?我知道处理 MEF 似乎存在的这些隔夜框架(缺乏文档、错误等)的风险,但 Prism 对我来说不是一个选择(当我可以使用我的手指)。
谢谢
最佳答案
为将来引用,Visual Mefx 的更新版本附在此博客文章中:How to Debug and Diagnose MEF Failures .它也是 MEFContrib 的一部分,尽管我不确定 MEFContrib 版本是否具有博客文章中版本的所有更改。
关于mvvm - Silverlight 4,MEF,导出/导入错误,Mefx 不想工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4441694/