Added new PrismBootstrapper API

This commit is contained in:
Brian Lagunas
2020-04-21 11:23:06 -06:00
parent af4811b1e1
commit eba083ee04
11 changed files with 372 additions and 2 deletions
+7
View File
@@ -0,0 +1,7 @@
<Application x:Class="HelloWorld.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
</Application.Resources>
</Application>
@@ -0,0 +1,18 @@
using System.Windows;
namespace HelloWorld
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var bs = new Bootstrapper();
bs.Run();
}
}
}
@@ -0,0 +1,21 @@
using HelloWorld.Views;
using Prism.Ioc;
using Prism.Unity;
using System.Windows;
using Unity;
namespace HelloWorld
{
class Bootstrapper : PrismBootstrapper
{
protected override Window CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
}
}
}
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UseWPF>true</UseWPF>
<RootNamespace>HelloWorld</RootNamespace>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\src\Prism.Core\Prism.Core.csproj" />
<ProjectReference Include="..\..\..\src\Wpf\Prism.Unity.Wpf\Prism.Unity.Wpf.csproj" />
<ProjectReference Include="..\..\..\src\Wpf\Prism.Wpf\Prism.Wpf.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,16 @@
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
namespace HelloWorld.ViewModels
{
public class MainWindowViewModel : BindableBase
{
public MainWindowViewModel()
{
}
}
}
@@ -0,0 +1,9 @@
<Window x:Class="HelloWorld.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True">
<Grid>
<TextBlock Text="Hello World" FontSize="48" />
</Grid>
</Window>
@@ -0,0 +1,15 @@
using System.Windows;
namespace HelloWorld.Views
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
+8 -2
View File
@@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28729.10
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HelloWorld", "HelloWorld\HelloWorld.csproj", "{B03C14CC-8DE9-40EE-9562-12B976E4CEE8}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HelloWorld", "HelloWorld\HelloWorld.csproj", "{B03C14CC-8DE9-40EE-9562-12B976E4CEE8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prism", "..\..\src\Prism.Core\Prism.Core.csproj", "{457AA668-72BB-4701-9A4E-FA86B0C412DE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prism.Core", "..\..\src\Prism.Core\Prism.Core.csproj", "{457AA668-72BB-4701-9A4E-FA86B0C412DE}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Prism Library", "Prism Library", "{15CF1FE1-D78E-4E3D-A9F8-FA0FCC56A83A}"
EndProject
@@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prism.DryIoc.Wpf", "..\..\s
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prism.Unity.Wpf", "..\..\src\Wpf\Prism.Unity.Wpf\Prism.Unity.Wpf.csproj", "{DEBADAAB-5C78-444E-AA77-336A43B49EC3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HelloWorld.Bootstrapper", "HelloWorld.Bootstraper\HelloWorld.Bootstrapper.csproj", "{36C11381-D25A-4E40-956F-05E9C440FA86}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -41,6 +43,10 @@ Global
{DEBADAAB-5C78-444E-AA77-336A43B49EC3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DEBADAAB-5C78-444E-AA77-336A43B49EC3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DEBADAAB-5C78-444E-AA77-336A43B49EC3}.Release|Any CPU.Build.0 = Release|Any CPU
{36C11381-D25A-4E40-956F-05E9C440FA86}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{36C11381-D25A-4E40-956F-05E9C440FA86}.Debug|Any CPU.Build.0 = Debug|Any CPU
{36C11381-D25A-4E40-956F-05E9C440FA86}.Release|Any CPU.ActiveCfg = Release|Any CPU
{36C11381-D25A-4E40-956F-05E9C440FA86}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -0,0 +1,27 @@
using DryIoc;
using Prism.Ioc;
using System;
namespace Prism.DryIoc
{
public abstract class PrismBootstrapper : PrismBootstrapperBase
{
/// <summary>
/// Create <see cref="Rules" /> to alter behavior of <see cref="IContainer" />
/// </summary>
/// <returns>An instance of <see cref="Rules" /></returns>
protected virtual Rules CreateContainerRules() => Rules.Default.WithAutoConcreteTypeResolution()
.With(Made.Of(FactoryMethod.ConstructorWithResolvableArguments))
.WithDefaultIfAlreadyRegistered(IfAlreadyRegistered.Replace);
protected override IContainerExtension CreateContainerExtension()
{
return new DryIocContainerExtension(new Container(CreateContainerRules()));
}
protected override void RegisterFrameworkExceptionTypes()
{
ExceptionExtensions.RegisterFrameworkExceptionType(typeof(ContainerException));
}
}
}
@@ -0,0 +1,19 @@
using Prism.Ioc;
using System;
using Unity;
namespace Prism.Unity
{
public abstract class PrismBootstrapper : PrismBootstrapperBase
{
protected override IContainerExtension CreateContainerExtension()
{
return new UnityContainerExtension();
}
protected override void RegisterFrameworkExceptionTypes()
{
ExceptionExtensions.RegisterFrameworkExceptionType(typeof(ResolutionFailedException));
}
}
}
+216
View File
@@ -0,0 +1,216 @@
using Prism.Events;
using Prism.Ioc;
using Prism.Logging;
using Prism.Modularity;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Regions.Behaviors;
using Prism.Services.Dialogs;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
namespace Prism
{
public abstract class PrismBootstrapperBase
{
IContainerExtension _containerExtension;
IModuleCatalog _moduleCatalog;
/// <summary>
/// The dependency injection container used to resolve objects
/// </summary>
public IContainerProvider Container => _containerExtension;
/// <summary>
/// Gets the shell user interface
/// </summary>
/// <value>The shell user interface.</value>
protected Window Shell { get; set; }
public void Run()
{
InitializeInternal();
}
/// <summary>
/// Run the initialization process.
/// </summary>
void InitializeInternal()
{
ConfigureViewModelLocator();
Initialize();
OnInitialized();
}
/// <summary>
/// Configures the <see cref="Prism.Mvvm.ViewModelLocator"/> used by Prism.
/// </summary>
protected virtual void ConfigureViewModelLocator()
{
ViewModelLocationProvider.SetDefaultViewModelFactory((view, type) =>
{
return Container.Resolve(type);
});
}
/// <summary>
/// Runs the initialization sequence to configure the Prism application.
/// </summary>
public virtual void Initialize()
{
ContainerLocator.SetContainerExtension(CreateContainerExtension);
_containerExtension = ContainerLocator.Current;
_moduleCatalog = CreateModuleCatalog();
RegisterRequiredTypes(_containerExtension);
RegisterTypes(_containerExtension);
_containerExtension.FinalizeExtension();
ConfigureModuleCatalog(_moduleCatalog);
var regionAdapterMappins = _containerExtension.Resolve<RegionAdapterMappings>();
ConfigureRegionAdapterMappings(regionAdapterMappins);
var defaultRegionBehaviors = _containerExtension.Resolve<IRegionBehaviorFactory>();
ConfigureDefaultRegionBehaviors(defaultRegionBehaviors);
RegisterFrameworkExceptionTypes();
var shell = CreateShell();
if (shell != null)
{
RegionManager.SetRegionManager(shell, _containerExtension.Resolve<IRegionManager>());
RegionManager.UpdateRegions();
InitializeShell(shell);
}
InitializeModules();
}
/// <summary>
/// Creates the container used by Prism.
/// </summary>
/// <returns>The container</returns>
protected abstract IContainerExtension CreateContainerExtension();
/// <summary>
/// Creates the <see cref="IModuleCatalog"/> used by Prism.
/// </summary>
/// <remarks>
/// The base implementation returns a new ModuleCatalog.
/// </remarks>
protected virtual IModuleCatalog CreateModuleCatalog()
{
return new ModuleCatalog();
}
/// <summary>
/// Registers all types that are required by Prism to function with the container.
/// </summary>
/// <param name="containerRegistry"></param>
protected virtual void RegisterRequiredTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterInstance(_moduleCatalog);
containerRegistry.RegisterSingleton<ILoggerFacade, TextLogger>();
containerRegistry.RegisterSingleton<IDialogService, DialogService>();
containerRegistry.RegisterSingleton<IModuleInitializer, ModuleInitializer>();
containerRegistry.RegisterSingleton<IModuleManager, ModuleManager>();
containerRegistry.RegisterSingleton<RegionAdapterMappings>();
containerRegistry.RegisterSingleton<IRegionManager, RegionManager>();
containerRegistry.RegisterSingleton<IRegionNavigationContentLoader, RegionNavigationContentLoader>();
containerRegistry.RegisterSingleton<IEventAggregator, EventAggregator>();
containerRegistry.RegisterSingleton<IRegionViewRegistry, RegionViewRegistry>();
containerRegistry.RegisterSingleton<IRegionBehaviorFactory, RegionBehaviorFactory>();
containerRegistry.Register<IRegionNavigationJournalEntry, RegionNavigationJournalEntry>();
containerRegistry.Register<IRegionNavigationJournal, RegionNavigationJournal>();
containerRegistry.Register<IRegionNavigationService, RegionNavigationService>();
containerRegistry.Register<IDialogWindow, DialogWindow>(); //default dialog host
}
/// <summary>
/// Used to register types with the container that will be used by your application.
/// </summary>
protected abstract void RegisterTypes(IContainerRegistry containerRegistry);
/// <summary>
/// Configures the <see cref="IRegionBehaviorFactory"/>.
/// This will be the list of default behaviors that will be added to a region.
/// </summary>
protected virtual void ConfigureDefaultRegionBehaviors(IRegionBehaviorFactory regionBehaviors)
{
if (regionBehaviors != null)
{
regionBehaviors.AddIfMissing(BindRegionContextToDependencyObjectBehavior.BehaviorKey, typeof(BindRegionContextToDependencyObjectBehavior));
regionBehaviors.AddIfMissing(RegionActiveAwareBehavior.BehaviorKey, typeof(RegionActiveAwareBehavior));
regionBehaviors.AddIfMissing(SyncRegionContextWithHostBehavior.BehaviorKey, typeof(SyncRegionContextWithHostBehavior));
regionBehaviors.AddIfMissing(RegionManagerRegistrationBehavior.BehaviorKey, typeof(RegionManagerRegistrationBehavior));
regionBehaviors.AddIfMissing(RegionMemberLifetimeBehavior.BehaviorKey, typeof(RegionMemberLifetimeBehavior));
regionBehaviors.AddIfMissing(ClearChildViewsRegionBehavior.BehaviorKey, typeof(ClearChildViewsRegionBehavior));
regionBehaviors.AddIfMissing(AutoPopulateRegionBehavior.BehaviorKey, typeof(AutoPopulateRegionBehavior));
regionBehaviors.AddIfMissing(IDestructibleRegionBehavior.BehaviorKey, typeof(IDestructibleRegionBehavior));
}
}
/// <summary>
/// Configures the default region adapter mappings to use in the application, in order
/// to adapt UI controls defined in XAML to use a region and register it automatically.
/// May be overwritten in a derived class to add specific mappings required by the application.
/// </summary>
/// <returns>The <see cref="RegionAdapterMappings"/> instance containing all the mappings.</returns>
protected virtual void ConfigureRegionAdapterMappings(RegionAdapterMappings regionAdapterMappings)
{
if (regionAdapterMappings != null)
{
regionAdapterMappings.RegisterMapping(typeof(Selector), _containerExtension.Resolve<SelectorRegionAdapter>());
regionAdapterMappings.RegisterMapping(typeof(ItemsControl), _containerExtension.Resolve<ItemsControlRegionAdapter>());
regionAdapterMappings.RegisterMapping(typeof(ContentControl), _containerExtension.Resolve<ContentControlRegionAdapter>());
}
}
/// <summary>
/// Registers the <see cref="Type"/>s of the Exceptions that are not considered
/// root exceptions by the <see cref="ExceptionExtensions"/>.
/// </summary>
protected virtual void RegisterFrameworkExceptionTypes()
{
}
/// <summary>
/// Creates the shell or main window of the application.
/// </summary>
/// <returns>The shell of the application.</returns>
protected abstract Window CreateShell();
/// <summary>
/// Initializes the shell.
/// </summary>
protected virtual void InitializeShell(Window shell)
{
Shell = shell;
}
/// <summary>
/// Contains actions that should occur last.
/// </summary>
protected virtual void OnInitialized()
{
Shell?.Show();
}
/// <summary>
/// Configures the <see cref="IModuleCatalog"/> used by Prism.
/// </summary>
protected virtual void ConfigureModuleCatalog(IModuleCatalog moduleCatalog) { }
/// <summary>
/// Initializes the modules.
/// </summary>
protected virtual void InitializeModules()
{
IModuleManager manager = _containerExtension.Resolve<IModuleManager>();
manager.Run();
}
}
}