2014-11-30 07:57:08 -08:00
|
|
|
|
using Ninject;
|
|
|
|
|
using Stylet;
|
|
|
|
|
using System;
|
2015-09-24 09:48:40 -07:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Reflection;
|
2015-01-14 04:23:43 -08:00
|
|
|
|
using System.Windows;
|
2014-11-30 07:57:08 -08:00
|
|
|
|
|
|
|
|
|
namespace Bootstrappers
|
|
|
|
|
{
|
2015-01-15 02:06:00 -08:00
|
|
|
|
public class NinjectBootstrapper<TRootViewModel> : BootstrapperBase where TRootViewModel : class
|
2014-11-30 07:57:08 -08:00
|
|
|
|
{
|
|
|
|
|
private IKernel kernel;
|
|
|
|
|
|
2015-01-15 02:06:00 -08:00
|
|
|
|
private object _rootViewModel;
|
2016-01-27 04:30:26 -08:00
|
|
|
|
protected virtual object RootViewModel
|
2015-01-15 02:06:00 -08:00
|
|
|
|
{
|
|
|
|
|
get { return this._rootViewModel ?? (this._rootViewModel = this.GetInstance(typeof(TRootViewModel))); }
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-30 07:57:08 -08:00
|
|
|
|
protected override void ConfigureBootstrapper()
|
|
|
|
|
{
|
|
|
|
|
this.kernel = new StandardKernel();
|
|
|
|
|
this.DefaultConfigureIoC(this.kernel);
|
|
|
|
|
this.ConfigureIoC(this.kernel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Carries out default configuration of the IoC container. Override if you don't want to do this
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual void DefaultConfigureIoC(IKernel kernel)
|
|
|
|
|
{
|
2016-07-09 04:21:02 -07:00
|
|
|
|
var viewManagerConfig = new ViewManagerConfig()
|
|
|
|
|
{
|
|
|
|
|
ViewFactory = this.GetInstance,
|
|
|
|
|
ViewAssemblies = new List<Assembly>() { this.GetType().Assembly }
|
|
|
|
|
};
|
|
|
|
|
kernel.Bind<IViewManager>().ToConstant(new ViewManager(viewManagerConfig));
|
2015-09-25 07:19:23 -07:00
|
|
|
|
|
2016-11-25 09:04:50 -08:00
|
|
|
|
kernel.Bind<IWindowManagerConfig>().ToConstant(this).InTransientScope();
|
2015-03-30 07:02:07 -07:00
|
|
|
|
kernel.Bind<IWindowManager>().ToMethod(c => new WindowManager(c.Kernel.Get<IViewManager>(), () => c.Kernel.Get<IMessageBoxViewModel>(), c.Kernel.Get<IWindowManagerConfig>())).InSingletonScope();
|
2014-11-30 07:57:08 -08:00
|
|
|
|
kernel.Bind<IEventAggregator>().To<EventAggregator>().InSingletonScope();
|
|
|
|
|
kernel.Bind<IMessageBoxViewModel>().To<MessageBoxViewModel>(); // Not singleton!
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Override to add your own types to the IoC container.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual void ConfigureIoC(IKernel kernel) { }
|
|
|
|
|
|
2014-12-02 07:20:16 -08:00
|
|
|
|
public override object GetInstance(Type type)
|
2014-11-30 07:57:08 -08:00
|
|
|
|
{
|
2014-12-02 04:52:58 -08:00
|
|
|
|
return this.kernel.Get(type);
|
2014-11-30 07:57:08 -08:00
|
|
|
|
}
|
2015-01-14 04:23:43 -08:00
|
|
|
|
|
2016-07-09 04:21:02 -07:00
|
|
|
|
protected override void Launch()
|
2016-01-27 04:30:26 -08:00
|
|
|
|
{
|
|
|
|
|
base.DisplayRootView(this.RootViewModel);
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-09 04:39:21 -08:00
|
|
|
|
public override void Dispose()
|
2015-01-14 04:23:43 -08:00
|
|
|
|
{
|
2016-01-27 04:30:26 -08:00
|
|
|
|
ScreenExtensions.TryDispose(this._rootViewModel);
|
|
|
|
|
if (this.kernel != null)
|
|
|
|
|
this.kernel.Dispose();
|
2018-09-30 10:57:12 -07:00
|
|
|
|
|
|
|
|
|
base.Dispose();
|
2015-01-14 04:23:43 -08:00
|
|
|
|
}
|
2014-11-30 07:57:08 -08:00
|
|
|
|
}
|
|
|
|
|
}
|