Search code examples
wpfdependency-injectioncaliburn.microsimple-injector

Simple Injector + Caliburn Micro 2.0 , error in resolving the view


I've got a strange problem with SimpleInjector and Caliburn Micro 2.0 .... I got an exception telling

"Cannot locate resource 'views/simpleviewmodel.xaml'."} 

The resource is set as

Build Action : Page

If I set it to resource (loosing the .cs) it loads...

My bootstrapper is

public class MefBootstrapper : BootstrapperBase
{
    public static readonly Container ContainerInstance = new Container();


    public MefBootstrapper()
    {
        Initialize();
    }

    protected override void Configure()
    {
        ContainerInstance.Register<IWindowManager, WindowManager>();
        ContainerInstance.RegisterSingle<IEventAggregator, EventAggregator>();

        ContainerInstance.Register<SimpleViewModel, SimpleViewModel>();

        ContainerInstance.Verify();
    }

    protected override object GetInstance(Type serviceType, string key)
    {

        return ContainerInstance.GetInstance(serviceType);
    }

    protected override IEnumerable<object> GetAllInstances(Type serviceType)
    {

        return ContainerInstance.GetAllInstances(serviceType);
    }

    protected override void BuildUp(object instance)
    {
        ContainerInstance.InjectProperties(instance);
    }

    protected override void OnStartup(object sender, StartupEventArgs e)
    {
        DisplayRootViewFor<SimpleViewModel>();
    }
}

The SimpleViewModel is really simple

 public class SimpleViewModel : Screen
 {
 }

and the view is simple as well

<UserControl x:Class="SimpleInjector.Views.SimpleView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <TextBlock Text="IT works!" Foreground="Red"></TextBlock>    </Grid>
</UserControl>

I've uploaded a project to test it yourself... if in the MefBoostrapper you comment the whole bootstrapper then uncomment the other one it works...so I think it's a problem related to SimpleInjector

Any help would be appreciated Thanks


Solution

  • Your problem has actually nothing to do with SimpleInjector. The problem is that you named your assembly SimpleInjector. So when the program builds it will create a SimpleInjector.exe file. This works because simpleinjector is a class library and thus is named .dll.

    But when Caliburn will search for the view, it searches for namespace SimpleInjector in an assembly SimpleInjector. But it has two of those (.dll and .exe), and will search in SimpleInjector.dll for this view, which it will not find! And there is your exception.

    Just rename your assembly output file to SimpleInjector.TestApp or something. You probably know this, but to be complete: You can change this in the properties of your project on the tabpage Application, field Assembly Name