Start of implementing ApplicationLoader

This commit is contained in:
Antony Male 2014-08-02 17:02:28 +01:00
parent bc9362eeee
commit fb9acc79c5
5 changed files with 97 additions and 48 deletions

View File

@ -11,20 +11,14 @@ namespace Stylet
public class Bootstrapper<TRootViewModel> : BootstrapperBase<TRootViewModel> where TRootViewModel : class
{
/// <summary>
/// Create a new Bootstrapper, which automatically starts and launches on application startup
/// Create a new Bootstrapper
/// </summary>
public Bootstrapper() : base() { }
/// <summary>
/// Create a new Bootstrapper, and specify whether to auto-start and auto-launch
/// </summary>
/// <param name="autoStart">True to call this.Start() on Application.Startup. If false, call this.Start() in your OnStartup override</param>
public Bootstrapper(bool autoStart) : base(autoStart) { }
/// <summary>
/// IoC container. This is created after ConfigureIoC has been run.
/// </summary>
protected IContainer container;
protected IContainer Container { get; private set; }
/// <summary>
/// Overridden from BootstrapperBase, this sets up the IoC container
@ -38,7 +32,7 @@ namespace Stylet
this.DefaultConfigureIoC(builder);
this.ConfigureIoC(builder);
this.container = builder.BuildContainer();
this.Container = builder.BuildContainer();
}
/// <summary>
@ -67,7 +61,7 @@ namespace Stylet
/// </summary>
protected override object GetInstance(Type service, string key = null)
{
return this.container.Get(service, key);
return this.Container.Get(service, key);
}
/// <summary>
@ -75,7 +69,7 @@ namespace Stylet
/// </summary>
protected override IEnumerable<object> GetAllInstances(Type service)
{
return this.container.GetAll(service);
return this.Container.GetAll(service);
}
/// <summary>
@ -83,7 +77,7 @@ namespace Stylet
/// </summary>
protected override void BuildUp(object instance)
{
this.container.BuildUp(instance);
this.Container.BuildUp(instance);
}
}
}

View File

@ -13,7 +13,7 @@ namespace Stylet
/// Bootstrapper to be extended by applications which don't want to use StyletIoC as the IoC container.
/// </summary>
/// <typeparam name="TRootViewModel">Type of the root ViewModel. This will be instantiated and displayed</typeparam>
public abstract class BootstrapperBase<TRootViewModel> where TRootViewModel : class
public abstract class BootstrapperBase<TRootViewModel> : IBootstrapper where TRootViewModel : class
{
/// <summary>
/// Reference to the current application
@ -21,18 +21,12 @@ namespace Stylet
protected Application Application { get; private set; }
/// <summary>
/// Create a new BootstrapperBase, which automatically starts on application startup
/// Called by the ApplicationLoader when this bootstrapper is loaded
/// </summary>
public BootstrapperBase() : this(true) { }
/// <summary>
/// Create a new BootstrapperBase, and specify whether to auto-start
/// </summary>
/// <param name="autoStart">True to call this.Start() at the end of this constructor</param>
[SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors", Justification = "We give the user the option to not call the virtual method, and call it themselves, if they want/need to")]
public BootstrapperBase(bool autoStart)
/// <param name="application"></param>
public void Setup(Application application)
{
this.Application = Application.Current;
this.Application = application;
// Allows for unit testing
if (this.Application != null)
@ -40,8 +34,7 @@ namespace Stylet
this.Application.Startup += (o, e) =>
{
this.OnApplicationStartup(o, e);
if (autoStart)
this.Start();
this.Start();
};
// Make life nice for the app - they can handle these by overriding Bootstrapper methods, rather than adding event handlers
@ -56,8 +49,7 @@ namespace Stylet
/// <summary>
/// Called from the constructor, this does everything necessary to start the application
/// </summary>
/// <param name="autoLaunch">True to automatically launch the main window</param>
protected virtual void Start(bool autoLaunch = true)
protected virtual void Start()
{
// Stitch the IoC shell to us
IoC.GetInstance = this.GetInstance;
@ -72,14 +64,11 @@ namespace Stylet
AssemblySource.Assemblies.Clear();
AssemblySource.Assemblies.AddRange(this.SelectAssemblies());
this.ConfigureResources();
this.Configure();
this.OnStart();
View.ViewManager = IoC.Get<IViewManager>();
if (autoLaunch && !Execute.InDesignMode)
if (!Execute.InDesignMode)
this.Launch();
}
@ -91,18 +80,6 @@ namespace Stylet
IoC.Get<IWindowManager>().ShowWindow(IoC.Get<TRootViewModel>());
}
/// <summary>
/// Add any application resources to the application. Override to add your own, or to avoid Stylet's default resources from being added
/// </summary>
protected virtual void ConfigureResources()
{
if (this.Application == null)
return;
var rc = new ResourceDictionary() { Source = new Uri("pack://application:,,,/Stylet;component/Xaml/StyletResourceDictionary.xaml", UriKind.Absolute) };
this.Application.Resources.MergedDictionaries.Add(rc);
}
/// <summary>
/// Override to configure your IoC container, and anything else
/// </summary>
@ -143,11 +120,6 @@ namespace Stylet
/// </summary>
protected virtual void OnApplicationStartup(object sender, StartupEventArgs e) { }
/// <summary>
/// Hook called when the application has started, and Stylet is fully initialised
/// </summary>
protected virtual void OnStart() { }
/// <summary>
/// Hook called on application exit
/// </summary>

22
Stylet/IBootstrapper.cs Normal file
View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace Stylet
{
/// <summary>
/// Interface specifying a bootstrapper, as loaded by the ApplicationLoader
/// </summary>
public interface IBootstrapper
{
/// <summary>
/// Called during application setup, allowing the bootstrapper to configure itself.
/// Should hook into Application.Startup
/// </summary>
/// <param name="application">Reference to the current application</param>
void Setup(Application application);
}
}

View File

@ -47,8 +47,10 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="Xaml\ApplicationLoader.cs" />
<Compile Include="ConductorNavigating.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="IBootstrapper.cs" />
<Compile Include="IValidationAdapter.cs" />
<Compile Include="LabelledValue.cs" />
<Compile Include="INotifyPropertyChangedDispatcher.cs" />

View File

@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace Stylet.Xaml
{
/// <summary>
/// Added to your App.xaml, this is responsible for loading the Boostrapper you specify, and Stylet's other resources
/// </summary>
public class ApplicationLoader : ResourceDictionary
{
private ResourceDictionary styletResourceDictionary;
/// <summary>
/// Create a new ApplicationLoader instance
/// </summary>
public ApplicationLoader()
{
this.styletResourceDictionary = new ResourceDictionary() { Source = new Uri("pack://application:,,,/Stylet;component/Xaml/StyletResourceDictionary.xaml", UriKind.Absolute) };
this.LoadStyletResources = true;
}
private IBootstrapper _bootstrapper;
/// <summary>
/// Bootstrapper instance to use to start your application. This must be set.
/// </summary>
public IBootstrapper Bootstrapper
{
get { return _bootstrapper; }
set
{
this._bootstrapper = value;
this._bootstrapper.Setup(Application.Current);
}
}
private bool _loadStyletResources;
/// <summary>
/// Control whether to load Stylet's own resources (e.g. StyletConductorTabControl). Defaults to true.
/// </summary>
public bool LoadStyletResources
{
get { return this._loadStyletResources; }
set
{
this._loadStyletResources = value;
if (this._loadStyletResources)
this.MergedDictionaries.Add(this.styletResourceDictionary);
else
this.MergedDictionaries.Remove(this.styletResourceDictionary);
}
}
}
}