WPFGermany
WPFGermany

Reputation: 1649

Caliburn Micro ViewLocator with different namespace

I'm using Caliburn Micro for MVVM. Now I have the following situation. I have a UserControl with View and ViewModel in my first assembly assembly1 in namespace1. If I use it in an second assembly assembly2 that has the same namespace namespace1 (it´s in the same solution) everything works fine.

Now I'd like to use my ViewModel in another Solution with namespace namespace3. If I try this I always get the error, that View couldn't be located.

I build up a workaround that sets the Binding manually in the bootstrapper (using Ninject).

protected override void Configure()
{
    _kernel = new StandardKernel();
    _kernel.Bind<OverlayManagerView>().To<OverlayManagerView>().InSingletonScope();
    _kernel.Bind<OverlayManagerViewModel>().To<OverlayManagerViewModel>().InSingletonScope();
    base.Configure();
}

protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
{
    ViewModelBinder.Bind(IoC.Get<OverlayManagerViewModel>(), IoC.Get<OverlayManagerView>(), null);
...
}

This is working, but if I'd like to use my ViewModels from assembly1 I won't always set the Binding manually and as Singleton.

Is there a way to tell the Caliburn ViewLocator that Views might be at a different namespace?

I tried following not working...

        ViewLocator.AddNamespaceMapping("namespace1", "namespace3");
        ViewLocator.AddNamespaceMapping("namespace1", "namespace1");
        ViewLocator.AddNamespaceMapping("namespace3", "namespace1");

Maybe someone knows a solution.

Upvotes: 2

Views: 1035

Answers (1)

Dragos Stoica
Dragos Stoica

Reputation: 1935

In your Configuremethod you should use :

ViewLocator.AddSubNamespaceMapping("ViewModelsNamespace", "ViewsNamespace");

and you have to override the following method :

    protected override IEnumerable<Assembly> SelectAssemblies()
    {

        var assemblies = new List<Assembly>();
        assemblies.AddRange(base.SelectAssemblies());
        //Load new ViewModels here
        string[] fileEntries = Directory.GetFiles(Directory.GetCurrentDirectory());

        assemblies.AddRange(from fileName in fileEntries
                            where fileName.Contains("ViewModels.dll")
                            select Assembly.LoadFile(fileName));
        assemblies.AddRange(from fileName in fileEntries
                            where fileName.Contains("Views.dll")
                            select Assembly.LoadFile(fileName));
        return assemblies;


    }

in order to let Caliburn know about your new dlls.

Upvotes: 2

Related Questions