Imports & exports in 1 assembly

Feb 25, 2010 at 9:41 PM
Edited Feb 25, 2010 at 9:43 PM

Hi there,

I've been playing with the excelent RiaXBoxgames example from Shaun Wildermuth. Today I wanted to examine how I could use this in the real world. What I tried was a library with views that I could use in the client. Everything worked like I'd expected. But when I wanted to combine the view- and the viewmodel library I recieved an errormessage: No valid exports were found that match the constraint

When I examine the Parts collection in the CompositionContainer, I can see the ViewModel has been found....  The problem I had looked much like an issue I found in the Issue tracker: http://mef.codeplex.com/WorkItem/View.aspx?WorkItemId=9764 

Can somebody tell me why I can't satisfy the imports when the exports are in the same assembly? And is there a workaround or solution? I've posted my code below (Well 100% rip-off from Shaun Wildermuth's project.)

Thanks & best regards,

Ron

// ViewModel    
[PartCreationPolicy(CreationPolicy.NonShared)]
[Export(ViewModelTypes.LoginViewModel)]
public class LoginViewModel : MyViewModelBase
{
    [ImportingConstructor] 
    public LoginViewModel(IClientModel clientModel)
    {
        // .....
    }
}

// View
public partial class LoginView : Page
{
    public LoginView()
    {
        InitializeComponent();
        if (!ViewModelBase.IsInDesignModeStatic)
        {
            // Use MEF To load the View Model
            PartInitializer.SatisfyImports(this);
        }
    }

    [Import(ViewModelTypes.LoginViewModel)]
    public LoginViewModel ViewModel
    {
        set
        {
            DataContext = value;
        }
    }
}

//Contract
public sealed class ViewModelTypes
{
    public const string LoginViewModel = "LoginViewModel";
}

Feb 25, 2010 at 11:20 PM

Have you read Diagnosing Composition Problems?

Developer
Feb 26, 2010 at 1:21 AM

Do you know if the assembly with the Export is included in the xap or loaded dynamically? PartInitializer.SatisfyImports works by loading a catalog from the assemblies included in the application xap and if the type with your Export is not in that xap then it will not be found.

If that isn't the issue then one thing that may be worth trying is removing the string contract name and simply use [Export] and [Import] respectively with no parameters. I'm simply curious if there is a bug with the string contract names here.

Feb 26, 2010 at 1:39 PM

Hi..... Feeling a bit silly now. The problem I thought I had wasn't the problem. The Export of the ViewModel was included in the xap. But the Export of the Model used bij the ViewModel wasn't in the xap after I played with it.

Thanks for the quick replies and sorry for the inconvenience... Just started with Silverlight and MEF but... I'm getting more and more enthousiastic with these technologies. :-)

Best regards, Ron