Search code examples
c#windows-store-appswindows-phone-8.1caliburn.micro

Caliburn Micro get service instance


I am using Caliburn Micro for WinRT apps. I have registered a service in my App.xaml.cs see sample code below

public sealed partial class App
    {
        private WinRTContainer _container;

      public App()
        {
            InitializeComponent();
            Resuming += AppResuming;
        }

        protected override void Configure()
        {
            _container = new WinRTContainer();

            _container.RegisterWinRTServices();

            _container.Singleton<ISampleService, SampleService>();
            //some more viewmodels & services added
        }

protected override void PrepareViewFirst(Frame rootFrame)
        {
            _container.RegisterNavigationService(rootFrame);
        }

protected override object GetInstance(Type service, string key)
        {
            return _container.GetInstance(service, key);
        }

        protected override IEnumerable<object> GetAllInstances(Type service)
        {
            return _container.GetAllInstances(service);
        }

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

Now, I have a utility class in which I want to get the instance of this SampleService.

How can I get the instance?


Solution

  • I'm supposing you are using MEF with Caliburn.Micro

    So you can get an instance this way :

    SampleService service = IoC.Get<ISampleService>();
    

    Hope this will help you.