Issue with import in MEF MVVM Silverlight 4
I am using MEF, MVVM and Silverlight4 and below is my code
Main.cs:
using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.D开发者_运维百科iagnostics;
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;
}
}
}
Viewmodel:
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;
}
Below is the error i am getting, Please do help me out trace the same.
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 == "MainViewModel")', invalid exports may have been rejected.
Resulting in:
Cannot set import 'IProspectCommonApp.Client.Main.ViewModel (ContractName="MainViewModel")' on part 'IProspectCommonApp.Client.Main'.
Element: IProspectCommonApp.Client.Main.ViewModel (ContractName="MainViewModel") --> IProspectCommonApp.Client.Main
It is probably failing because there is no IAuthenticationModel and/or IprospectManagementModel exported. The MainViewModel imports these via the ImportingConstructor, so it can't be created if they haven't been exported.
For more information on MEF debugging, see How to Debug and Diagnose MEF Failures.
精彩评论