182 lines
8.8 KiB
C#
182 lines
8.8 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// https://msdn.microsoft.com/en-us/library/ff921075(v=pandp.20).aspx
|
|
/// </summary>
|
|
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<IUnityContainer>();
|
|
|
|
//if (Standalone)
|
|
//{ _unityContainer.RegisterType<IViewerMainView, ViewerMainView>(); _unityContainer.RegisterType<IViewerMainViewModel, ViewerMainViewModel>(new ContainerControlledLifetimeManager()); }
|
|
//else
|
|
//{ _unityContainer.RegisterType<IViewerMainViewGrid, ViewerMainViewGrid>(); _unityContainer.RegisterType<IViewerMainViewModel, ViewerMainViewModel>(new ContainerControlledLifetimeManager()); }
|
|
|
|
//_unityContainer.RegisterType<IViewerMainView, ViewerMainView>();
|
|
_unityContainer.RegisterType<IViewerMainViewGrid, ViewerMainViewGrid>();
|
|
_unityContainer.RegisterType<IViewerMainViewModel, ViewerMainViewModel>(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<RegionAdapterMappings>();
|
|
if (regionAdapterMappings != null)
|
|
{
|
|
try { regionAdapterMappings.RegisterMapping(typeof(System.Windows.Controls.Primitives.Selector), ServiceLocator.Current.GetInstance<SelectorRegionAdapter>()); } catch { }
|
|
try { regionAdapterMappings.RegisterMapping(typeof(ItemsControl), ServiceLocator.Current.GetInstance<ItemsControlRegionAdapter>()); } catch { }
|
|
try { regionAdapterMappings.RegisterMapping(typeof(ContentControl), ServiceLocator.Current.GetInstance<ContentControlRegionAdapter>()); } catch { }
|
|
}
|
|
|
|
if (Standalone) { try { regionAdapterMappings.RegisterMapping(typeof(StackPanel), ServiceLocator.Current.GetInstance<StackPanelRegionAdapter>()); } catch { } }
|
|
return regionAdapterMappings;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
protected override DependencyObject CreateShell()
|
|
{
|
|
try
|
|
{
|
|
Container = ServiceLocator.Current.GetInstance<IUnityContainer>();
|
|
_EventAggregator = Container.Resolve<IEventAggregator>();
|
|
_ServiceLocator = Container.Resolve<IServiceLocator>();
|
|
|
|
var viewModel = _unityContainer.TryResolve<IViewerMainViewModel>();
|
|
|
|
if (viewModel == null) return new DependencyObject();
|
|
|
|
var view = viewModel.View;
|
|
|
|
// Registering region
|
|
if (!Container.IsRegistered<IRegionManager>()) return ServiceLocator.Current.GetInstance<ViewerMainView>();
|
|
|
|
_regionManager = Container.Resolve<IRegionManager>();
|
|
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<IShellViewModel>();
|
|
viewModel.Initialize(parent);
|
|
parent.ContextMainRegion = view;
|
|
}
|
|
else
|
|
{
|
|
viewModel.Initialize();
|
|
}
|
|
return (DependencyObject)view; //ServiceLocator.Current.GetInstance<MainView>();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
var s = e.Message;
|
|
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Returns the module catalog that will be used to initialize the modules.
|
|
/// </summary>
|
|
/// <returns>
|
|
/// An instance of <see cref="IModuleCatalog"/> that will be used to initialize the modules.
|
|
/// </returns>
|
|
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<IUnityContainer>();
|
|
var catalog = Container.TryResolve<IModuleCatalog>();
|
|
return catalog ?? new AggregateModuleCatalog();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
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<IDTSViewRegionManager, DTSViewRegionManager>(new ContainerControlledLifetimeManager(), new InjectionMember[] { });
|
|
base.InitializeModules();
|
|
}
|
|
}
|
|
}
|