using System; using System.Collections.Specialized; using System.ComponentModel.Composition.Hosting; using System.Configuration; using System.IO; using System.Runtime.InteropServices.ComTypes; using System.Windows; using System.Windows.Controls; using DTS.Common; using DTS.Common.Base; using DTS.Common.Core.PluginLib; using DTS.Common.Interface; using Microsoft.Practices.Prism.Events; using Microsoft.Practices.Prism.Modularity; using Microsoft.Practices.Prism.Regions; using Microsoft.Practices.Prism.UnityExtensions; using Microsoft.Practices.ServiceLocation; using Microsoft.Practices.Unity; // ReSharper disable InconsistentNaming // ReSharper disable ConvertToAutoProperty namespace DTS.Viewer.PSDReport { /// /// https://msdn.microsoft.com/en-us/library/ff921075(v=pandp.20).aspx /// public class Bootstrapper : UnityBootstrapper { private IRegionManager _regionManager; private IUnityContainer _unityContainer => Container; public IServiceLocator _ServiceLocator { get; private set; } public IEventAggregator _EventAggregator { get; private set; } private bool _standalone; public bool Standalone { get => _standalone; set => _standalone = value; } private string _customConfigPath = string.Empty; public string CustomConfigPath { get => _customConfigPath; set => _customConfigPath = value; } public Bootstrapper(bool standalone, string customConfigPath = "") { _customConfigPath = customConfigPath; _standalone = standalone; } protected override void ConfigureContainer() { Container = ServiceLocator.Current.GetInstance(); //if (Standalone) //{ _unityContainer.RegisterType(); _unityContainer.RegisterType(new ContainerControlledLifetimeManager()); } //else //{ _unityContainer.RegisterType(); _unityContainer.RegisterType(new ContainerControlledLifetimeManager()); } //_unityContainer.RegisterType(); _unityContainer.RegisterType(); _unityContainer.RegisterType(new ContainerControlledLifetimeManager()); base.ConfigureContainer(); } protected override RegionAdapterMappings ConfigureRegionAdapterMappings() { //var regionAdapterMappings = base.ConfigureRegionAdapterMappings(); //re-implement base CRAM. doesn't catch already registered mappings //https://prismlibrary.com/docs/wpf/legacy/Appendix-D-Extending-Prism.html#customizing-the-region-adapter-mappings RegionAdapterMappings regionAdapterMappings = ServiceLocator.Current.GetInstance(); if (regionAdapterMappings != null) { try { regionAdapterMappings.RegisterMapping(typeof(System.Windows.Controls.Primitives.Selector), ServiceLocator.Current.GetInstance()); } catch { } try { regionAdapterMappings.RegisterMapping(typeof(ItemsControl), ServiceLocator.Current.GetInstance()); } catch { } try { regionAdapterMappings.RegisterMapping(typeof(ContentControl), ServiceLocator.Current.GetInstance()); } catch { } } if (Standalone) { try { regionAdapterMappings.RegisterMapping(typeof(StackPanel), ServiceLocator.Current.GetInstance()); } catch { } } return regionAdapterMappings; } /// /// The CreateShell method allows a developer to specify the top-level window for a Prism application. /// The shell is usually the MainWindow or MainPage. Implement this method by returning an instance of /// your application's shell class. In a Prism application, you can create the shell object, or resolve /// it from the container, depending on your application's requirements. /// /// protected override DependencyObject CreateShell() { try { Container = ServiceLocator.Current.GetInstance(); _EventAggregator = Container.Resolve(); _ServiceLocator = Container.Resolve(); var viewModel = _unityContainer.TryResolve(); if (viewModel == null) return new DependencyObject(); var view = viewModel.View; // Registering region if (!Container.IsRegistered()) return ServiceLocator.Current.GetInstance(); _regionManager = Container.Resolve(); foreach (var region in viewModel.GetRegions()) { if (_regionManager.Regions.ContainsRegionWithName(region.Name)) continue; _regionManager.Regions.Add(new Region { Name = region.Name, Context = region }); RegionManager.SetRegionManager(region, _regionManager); RegionManager.UpdateRegions(); } view.DataContext = viewModel; //((ViewerMainView)view).Visibility = Visibility.Visible; if (Standalone) { var parent = _unityContainer.Resolve(); viewModel.Initialize(parent); parent.ContextMainRegion = view; } else { viewModel.Initialize(); } return (DependencyObject)view; //ServiceLocator.Current.GetInstance(); } catch (Exception e) { var s = e.Message; } return null; } /// /// Returns the module catalog that will be used to initialize the modules. /// /// /// An instance of that will be used to initialize the modules. /// protected override IModuleCatalog CreateModuleCatalog() { // The module catalog is used by the ModuleManager and ModuleLoader components, which are responsible for downloading the modules. if (Container == null) Container = ServiceLocator.Current.GetInstance(); var catalog = Container.TryResolve(); return catalog ?? new AggregateModuleCatalog(); } /// /// Modules are packages of functionality that can be independently developed, tested, and (optionally) deployed. /// In a composite application, modules must be discovered and loaded at run time by the host application. /// The module catalog is used by the ModuleManager and ModuleLoader components, which are responsible for downloading the modules. /// Modules are copied to a directory as part of a post-build step. These modules are not referenced in the project and are discovered by inspecting a directory. /// Module projects have a post-build step to copy themselves into that directory. /// protected override void ConfigureModuleCatalog() { var pluginFolders = string.Empty; if (!Standalone) return; var applicationSettings = ConfigurationManager.GetSection("DTS.Common.Core.PluginLib.Config"); foreach (FilterHashElement key in ((PluginConfigSectionHandler)applicationSettings).HashKeys) { if (key.Key != "viewerPlugins") continue; pluginFolders = key.Value; break; } if (string.IsNullOrEmpty(pluginFolders)) return; var directoryCatalog = new DirectoryModuleCatalog { ModulePath = pluginFolders }; ((AggregateModuleCatalog)ModuleCatalog).AddCatalog(directoryCatalog); } protected override void InitializeModules() { if (!Standalone) return; // Register the DTSRegionManager with Unity dependency injection container as a singleton. Container.RegisterType(new ContainerControlledLifetimeManager(), new InjectionMember[] { }); base.InitializeModules(); } } }