Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot make my ViewModelLocator work

I have a ViewModel called MainViewModel (of course) which contains multiple Contructors as per the below:

[ImportingConstructor]
public MainViewModel(IWindowManager windowManager)
    : this(windowManager, new DataProvider(), new LocalJsonPersistenceManager())
{
}

[PreferredConstructorAttribute]
public MainViewModel(IWindowManager windowManager, IInformationProvider infoProvider,
                     IPersistenceManager persistenceManager)
{
    //generating data, handling clicks etc.
}

Inside that ViewModel is a public item that is constantly being updated (whenever a user clicks on a certain button and takes some actions on the form):

public Item ClickedItem
{
    get { return clickedItem; }
    set
        {
            clickedItem = value;
            NotifyOfPropertyChange(() => ClickedItem);
            if (ClickedItem != null)
            {
                FindNextItem();
            }
        }
}

Now i have a UserControl I am building that contains a ListView that I personnalised to make it a sticky headered listview (header moves up whenever the next header is reached blabla ...). because I can only do this via a GroupStyled ListView, I must build the data for the ListView in the C# code behind.

EDIT: I am trying it using a ViewModelLocator as such:

public class ViewModelLocator
    {
        public ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

            if (ViewModelBase.IsInDesignModeStatic)
            {
                // Create design time view services and models
                //SimpleIoc.Default.Register<IDataService, DesignDataService>();
            }
            else
            {
                // Create run time view services and models
                //SimpleIoc.Default.Register<IDataService, DataService>();
            }

            SimpleIoc.Default.Register<MainViewModel>();
        }


        public MainViewModel Main
        {
            get
            {
                return ServiceLocator.Current.GetInstance<MainViewModel>();
            }
        }

        public static void Cleanup()
        {
            // TODO Clear the ViewModels
        }
    }

And I am calling up the data's specific value as such:

var vm1 = (new ViewModelLocator()).Main;
            testtxt.Text = vm1.ClickedItem.Name;

But it keeps giving me an error message on runtime on the line:

        return ServiceLocator.Current.GetInstance<MainViewModel>();

in the ViewModelLocator's block:

public MainViewModel Main
        {
            get
            {
                return ServiceLocator.Current.GetInstance<MainViewModel>();
            }
        }

With the error message {"Type not found in cache: Caliburn.Micro.IWindowManager."} and an InnerException message of null.

like image 962
touyets Avatar asked Dec 15 '25 13:12

touyets


1 Answers

It looks like MEF cannot construct the IWindowManager, which is a dependency of your ViewModel.

Try registering at least the default instance from Caliburn.

Update

taken straight from caliburn.Micro's MEF-Bootstrapper:

container = CompositionHost.Initialize(
                new AggregateCatalog(
                    AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()
                    )
                );

var batch = new CompositionBatch();
batch.AddExportedValue<IWindowManager>(new WindowManager());
batch.AddExportedValue<IEventAggregator>(new EventAggregator());
batch.AddExportedValue(container);
container.Compose(batch);
like image 125
flq Avatar answered Dec 17 '25 02:12

flq