我正在使用MEF,MVVM和Silverlight4,以下是我的代码
Main.cs:
using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Diagnostics;
using System.ServiceModel.DomainServices.Client.ApplicationServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
public partial class Main : UserControl
{
public Main()
{
InitializeComponent();
// Satisfy the MEF imports for the class.
if (!DesignerProperties.IsInDesignTool)
{
CompositionInitializer.SatisfyImports(this);
}
}
/// <summary>
/// Sets the datacontext to the viewmodel for this view
/// </summary>
[Import(ViewModelTypes.MainViewModel)]
public object ViewModel
{
set
{
this.DataContext = value;
}
}
}
视图模型:
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Windows.Input;
[Export(ViewModelTypes.MainViewModel)]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class MainViewModel : ViewModelBase
{
[ImportingConstructor]
public MainViewModel(IAuthenticationModel authModel, IprospectManagementModel managementModel)
{
this.authenticationModel = authModel;
this.managementModel = managementModel;
}
/// <summary>
/// The authentication model.
/// </summary>
private IAuthenticationModel authenticationModel;
/// <summary>
/// The Iprospect management model.
/// </summary>
private IprospectManagementModel managementModel;
}
以下是我得到的错误,请帮我跟踪一下。
构图保持不变。由于以下错误,更改被拒绝:组合产生单个组合错误。根本原因如下。查看CompositionException.Errors属性以获取更多详细信息。
1)找不到与约束'(exportDefinition.ContractName ==“MainViewModel”)匹配的有效导出,无效导出可能已被拒绝。
导致:
Cannot set import 'IProspectCommonApp.Client.Main.ViewModel (ContractName="MainViewModel")' on part 'IProspectCommonApp.Client.Main'.
Element: IProspectCommonApp.Client.Main.ViewModel (ContractName="MainViewModel") --> IProspectCommonApp.Client.Main
答案 0 :(得分:1)
可能失败了,因为没有导出IAuthenticationModel和/或IprospectManagementModel。 MainViewModel通过ImportingConstructor导入它们,因此如果它们尚未导出,则无法创建它们。
有关MEF调试的更多信息,请参阅How to Debug and Diagnose MEF Failures。