Error trying to convert Caliburn.Micro.HelloScreens sample to WPF

Topics: Bootstrappers & IoC
Oct 12, 2012 at 3:23 PM

I am trying to learning Caliburn Micro and WPF.  I have an app that needs functionality similar to the HelloScreen sample but in WPF.  When I try the code in WPF I keep getting an error in the BootStrapper GetInstance

Could not locate any instances of contract WpfApplication.Framework.IShell.

protected override void Configure()
        {
            container = new CompositionContainer(
               new AggregateCatalog(AssemblySource.Instance.Select(x => new AssemblyCatalog(x)))
               );

            var batch = new CompositionBatch();

            batch.AddExportedValue(new WindowManager());
            batch.AddExportedValue(new EventAggregator());
            batch.AddExportedValue(container);

            container.Compose(batch);
        }
Assistance would be greatly appreciated.  Thanks!
Oct 12, 2012 at 9:37 PM

Just a couple things to check:

Does your ShellViewModel that implements IShell have the [Export(typeof(IShell))] attribute?

Does your ShellViewModel have a constructor that is expecting to get imports?  Does the constructor have the [ImportingConstructor] attribute, and are all the constructor arg classes marked with [Export] attributes?

Oct 12, 2012 at 9:41 PM
andywhite37 wrote:

Just a couple things to check:

Does your ShellViewModel that implements IShell have the [Export(typeof(IShell))] attribute?

Does your ShellViewModel have a constructor that is expecting to get imports?  Does the constructor have the [ImportingConstructor] attribute, and are all the constructor arg classes marked with [Export] attributes?


Yes they do.  Here is the code for the ShellViewModel.cs

[Export(typeof(IShell))]
    public class ShellViewModel : Conductor<IWorkspace>.Collection.OneActive, IShell
    {
        private readonly IDialogManager dialogs;

        [ImportingConstructor]
        public ShellViewModel(IDialogManager dialogs, [ImportMany]IEnumerable workspaces)
        {
            this.dialogs = dialogs;
            Items.AddRange(workspaces);
            CloseStrategy = new ApplicationCloseStrategy();
        }

        public IDialogManager Dialogs
        {
            get { return dialogs; }
        }
    }
Oct 12, 2012 at 10:15 PM

Maybe try changing the constructor to this:

public ShellViewModel(IDialogManager dialogs, [ImportMany]IEnumerable<IWorkspace> workspaces)

The IEnumerable may need a generic type so MEF knows what instances to inject.

Also make sure your IWorkspace view model classes have [Export(typeof(IWorkspace))]

Oct 12, 2012 at 10:24 PM

Sorry for some reason when I pasted the code it removed the type.  It does say

public ShellViewModel(IDialogManager dialogs, [ImportMany]IEnumerable<IWorkspace> workspaces)

IWorkspace.cs is an interface with

 public interface IWorkspace
    {
        string Icon { get; }
        string IconName { get; }
        string Status { get; }

        void Show();
    }

So errors if you add Export.  I have basically copied and pasted over from the sample Silverlight to WPF.  In the bootstrapper I commented out the throw exception and get this for a real error.

Sequence contains no elements

This occurs and the return exports.First(); 

string contract shows "IShell" and the exports container has a count of 0.  Something isnt quite working right in the bootstrapper in WPF.

 

Oct 16, 2012 at 7:36 PM

is IShell in the right namespace?

Oct 15, 2014 at 9:25 PM
Hi,
Has this been solved? I have the same problem.
Please post the solution if you solved it.