Bootstrapper.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. namespace Caliburn.Micro {
  2. using Caliburn.Micro.Core;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Windows;
  8. using System.Windows.Threading;
  9. /// <summary>
  10. /// Inherit from this class in order to customize the configuration of the framework.
  11. /// </summary>
  12. public abstract class BootstrapperBase {
  13. readonly bool useApplication;
  14. bool isInitialized;
  15. /// <summary>
  16. /// The application.
  17. /// </summary>
  18. protected Application Application { get; set; }
  19. /// <summary>
  20. /// Creates an instance of the bootstrapper.
  21. /// </summary>
  22. /// <param name="useApplication">Set this to false when hosting Caliburn.Micro inside and Office or WinForms application. The default is true.</param>
  23. protected BootstrapperBase(bool useApplication = true) {
  24. this.useApplication = useApplication;
  25. }
  26. /// <summary>
  27. /// Initialize the framework.
  28. /// </summary>
  29. public void Initialize() {
  30. if(isInitialized) {
  31. return;
  32. }
  33. isInitialized = true;
  34. PlatformProvider.Current = new XamlPlatformProvider();
  35. #if WP8 || NET45
  36. var baseExtractTypes = AssemblySourceCache.ExtractTypes;
  37. AssemblySourceCache.ExtractTypes = assembly =>
  38. {
  39. var baseTypes = baseExtractTypes(assembly);
  40. var elementTypes = assembly.GetExportedTypes()
  41. .Where(t => typeof(UIElement).IsAssignableFrom(t));
  42. return baseTypes.Union(elementTypes);
  43. };
  44. AssemblySource.Instance.Refresh();
  45. #endif
  46. if(Execute.InDesignMode) {
  47. try {
  48. StartDesignTime();
  49. }catch {
  50. //if something fails at design-time, there's really nothing we can do...
  51. isInitialized = false;
  52. throw;
  53. }
  54. }
  55. else {
  56. StartRuntime();
  57. }
  58. }
  59. /// <summary>
  60. /// Called by the bootstrapper's constructor at design time to start the framework.
  61. /// </summary>
  62. protected virtual void StartDesignTime() {
  63. AssemblySource.Instance.Clear();
  64. AssemblySource.Instance.AddRange(SelectAssemblies());
  65. Configure();
  66. IoC.GetInstance = GetInstance;
  67. IoC.GetAllInstances = GetAllInstances;
  68. IoC.BuildUp = BuildUp;
  69. }
  70. /// <summary>
  71. /// Called by the bootstrapper's constructor at runtime to start the framework.
  72. /// </summary>
  73. protected virtual void StartRuntime() {
  74. EventAggregator.HandlerResultProcessing = (target, result) => {
  75. var task = result as System.Threading.Tasks.Task;
  76. if (task != null) {
  77. result = new IResult[] {task.AsResult()};
  78. }
  79. var coroutine = result as IEnumerable<IResult>;
  80. if (coroutine != null) {
  81. var viewAware = target as IViewAware;
  82. var view = viewAware != null ? viewAware.GetView() : null;
  83. var context = new CoroutineExecutionContext { Target = target, View = view };
  84. Coroutine.BeginExecute(coroutine.GetEnumerator(), context);
  85. }
  86. };
  87. AssemblySourceCache.Install();
  88. AssemblySource.Instance.AddRange(SelectAssemblies());
  89. if (useApplication) {
  90. Application = Application.Current;
  91. PrepareApplication();
  92. }
  93. Configure();
  94. IoC.GetInstance = GetInstance;
  95. IoC.GetAllInstances = GetAllInstances;
  96. IoC.BuildUp = BuildUp;
  97. }
  98. /// <summary>
  99. /// Provides an opportunity to hook into the application object.
  100. /// </summary>
  101. protected virtual void PrepareApplication() {
  102. Application.Startup += OnStartup;
  103. #if SILVERLIGHT
  104. Application.UnhandledException += OnUnhandledException;
  105. #else
  106. Application.DispatcherUnhandledException += OnUnhandledException;
  107. #endif
  108. Application.Exit += OnExit;
  109. }
  110. /// <summary>
  111. /// Override to configure the framework and setup your IoC container.
  112. /// </summary>
  113. protected virtual void Configure() { }
  114. /// <summary>
  115. /// Override to tell the framework where to find assemblies to inspect for views, etc.
  116. /// </summary>
  117. /// <returns>A list of assemblies to inspect.</returns>
  118. protected virtual IEnumerable<Assembly> SelectAssemblies() {
  119. return new[] { GetType().Assembly };
  120. }
  121. /// <summary>
  122. /// Override this to provide an IoC specific implementation.
  123. /// </summary>
  124. /// <param name="service">The service to locate.</param>
  125. /// <param name="key">The key to locate.</param>
  126. /// <returns>The located service.</returns>
  127. protected virtual object GetInstance(Type service, string key) {
  128. #if NET
  129. if (service == typeof(IWindowManager))
  130. service = typeof(WindowManager);
  131. #endif
  132. return Activator.CreateInstance(service);
  133. }
  134. /// <summary>
  135. /// Override this to provide an IoC specific implementation
  136. /// </summary>
  137. /// <param name="service">The service to locate.</param>
  138. /// <returns>The located services.</returns>
  139. protected virtual IEnumerable<object> GetAllInstances(Type service) {
  140. return new[] { Activator.CreateInstance(service) };
  141. }
  142. /// <summary>
  143. /// Override this to provide an IoC specific implementation.
  144. /// </summary>
  145. /// <param name="instance">The instance to perform injection on.</param>
  146. protected virtual void BuildUp(object instance) { }
  147. /// <summary>
  148. /// Override this to add custom behavior to execute after the application starts.
  149. /// </summary>
  150. /// <param name="sender">The sender.</param>
  151. /// <param name="e">The args.</param>
  152. protected virtual void OnStartup(object sender, StartupEventArgs e) { }
  153. /// <summary>
  154. /// Override this to add custom behavior on exit.
  155. /// </summary>
  156. /// <param name="sender">The sender.</param>
  157. /// <param name="e">The event args.</param>
  158. protected virtual void OnExit(object sender, EventArgs e) { }
  159. #if SILVERLIGHT
  160. /// <summary>
  161. /// Override this to add custom behavior for unhandled exceptions.
  162. /// </summary>
  163. /// <param name="sender">The sender.</param>
  164. /// <param name="e">The event args.</param>
  165. protected virtual void OnUnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) { }
  166. #else
  167. /// <summary>
  168. /// Override this to add custom behavior for unhandled exceptions.
  169. /// </summary>
  170. /// <param name="sender">The sender.</param>
  171. /// <param name="e">The event args.</param>
  172. protected virtual void OnUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) { }
  173. #endif
  174. #if SILVERLIGHT && !WINDOWS_PHONE
  175. /// <summary>
  176. /// Locates the view model, locates the associate view, binds them and shows it as the root view.
  177. /// </summary>
  178. /// <param name="viewModelType">The view model type.</param>
  179. protected void DisplayRootViewFor(Type viewModelType) {
  180. var viewModel = IoC.GetInstance(viewModelType, null);
  181. var view = ViewLocator.LocateForModel(viewModel, null, null);
  182. ViewModelBinder.Bind(viewModel, view, null);
  183. var activator = viewModel as IActivate;
  184. if(activator != null)
  185. activator.Activate();
  186. Mouse.Initialize(view);
  187. Application.RootVisual = view;
  188. }
  189. /// <summary>
  190. /// Locates the view model, locates the associate view, binds them and shows it as the root view.
  191. /// </summary>
  192. /// <typeparam name="TViewModel">The view model type.</typeparam>
  193. protected void DisplayRootViewFor<TViewModel>() {
  194. DisplayRootViewFor(typeof(TViewModel));
  195. }
  196. #elif NET
  197. /// <summary>
  198. /// Locates the view model, locates the associate view, binds them and shows it as the root view.
  199. /// </summary>
  200. /// <param name="viewModelType">The view model type.</param>
  201. /// <param name="settings">The optional window settings.</param>
  202. protected void DisplayRootViewFor(Type viewModelType, IDictionary<string, object> settings = null) {
  203. var windowManager = IoC.Get<IWindowManager>();
  204. windowManager.ShowWindow(IoC.GetInstance(viewModelType, null), null, settings);
  205. }
  206. /// <summary>
  207. /// Locates the view model, locates the associate view, binds them and shows it as the root view.
  208. /// </summary>
  209. /// <typeparam name="TViewModel">The view model type.</typeparam>
  210. /// <param name="settings">The optional window settings.</param>
  211. protected void DisplayRootViewFor<TViewModel>(IDictionary<string, object> settings = null) {
  212. DisplayRootViewFor(typeof(TViewModel), settings);
  213. }
  214. #endif
  215. }
  216. }