Neil February 2016

WPF MVVM load View and ViewModel from external assembly

so I have inherited the development of a WPF application that uses Caliburn.Micro. I have been tasked to extend the application and include some AddIn functionality, I have coded the core logic for the AddIn feature which all works pretty well until I run the application and the shell view informs me that it "Cannot find view for TestViewModel"

Here is my Configure method in the bootstrapper

protected override void Configure()
{
    this._log.Debug("-->AppBootstrapper.Configure[ENTER]");
    try
    {
        SplashScreenForm.SplashScreen.Dispatcher.BeginInvoke(
                (Action)(() => SplashScreenForm.SplashScreen.Message = "Initializing Container..."));

        this._container = new CompositionContainer(new AggregateCatalog(new DirectoryCatalog(".", "*")));
        var batch = new CompositionBatch();

        SplashScreenForm.SplashScreen.Dispatcher.BeginInvoke(
                (Action)(() => SplashScreenForm.SplashScreen.Message = "Initializing Dependencies..."));
        batch.AddExportedValue<IWindowManager>(new WindowManager());
        batch.AddExportedValue<IEventAggregator>(new EventAggregator());

        batch.AddExportedValue(this._container);
        this._container.Compose(batch);
    }
    catch (Exception ex)
    {
        this._log.ErrorFormat("-->AppBootstrapper.Configure - {0}\n{1}", ex.Message, ex);
        throw new Exception(ex.Message, ex);
    }

    this._log.Debug("-->AppBootstrapper.Configure[EXIT]");
}

I then have two assemblies, AppMain which contains the main application logic (this has a ViewModels and Views folders and these all load fine), I also have an AppAddinTest assembly which contains a test addin, this also contains a ViewModels and Views folder.

My TestViewModel code is:

[Export(typeof(TestViewModel))]
public class TestViewModel : BaseViewModel, ITestViewModel
{
    private readonly IEventAggregator _eventAggregator;

            

Answers


EngineerSpock February 2016

I bet you haven't added the assembly on the stage of bootstrapping where your ViewModel resides. Override SelectAssemblies() in the bootstrapper:

protected override IEnumerable<Assembly> SelectAssemblies() {
        var assemblies = new List<Assembly> {
            //AddTheAssemblyWhereViewModelsReside
        };
        return assemblies;
    }

Post Status

Asked in February 2016
Viewed 2,471 times
Voted 10
Answered 1 times

Search




Leave an answer