mirror of https://github.com/AMT-Cheif/Stylet.git
Add super-simple IoC container
This commit is contained in:
parent
9543bb28c1
commit
7ea92e3673
|
@ -27,10 +27,27 @@ namespace Stylet
|
|||
|
||||
this.Application.Startup += (o, e) =>
|
||||
{
|
||||
new WindowManager().ShowWindow(Activator.CreateInstance(typeof(TRootViewModel)));
|
||||
IoC.Get<IWindowManager>().ShowWindow(IoC.Get<TRootViewModel>());
|
||||
};
|
||||
|
||||
IoC.GetInstance = this.GetInstance;
|
||||
IoC.GetAllInstances = this.GetAllInstances;
|
||||
IoC.BuildUp = this.BuildUp;
|
||||
}
|
||||
|
||||
protected virtual object GetInstance(Type service, string key = null)
|
||||
{
|
||||
if (service == typeof(IWindowManager)) service = typeof(WindowManager);
|
||||
return Activator.CreateInstance(service);
|
||||
}
|
||||
|
||||
protected virtual IEnumerable<object> GetAllInstances(Type service)
|
||||
{
|
||||
return new[] { Activator.CreateInstance(service) };
|
||||
}
|
||||
|
||||
protected virtual void BuildUp(object instance) { }
|
||||
|
||||
protected virtual void OnStartup(object sender, StartupEventArgs e) { }
|
||||
protected virtual void OnExit(object sender, EventArgs e) { }
|
||||
protected virtual void OnUnhandledExecption(object sender, DispatcherUnhandledExceptionEventArgs e) { }
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Stylet
|
||||
{
|
||||
public static class IoC
|
||||
{
|
||||
public static Func<Type, string, object> GetInstance = (service, key) => { throw new InvalidOperationException("IoC is not initialized"); };
|
||||
public static Func<Type, IEnumerable<object>> GetAllInstances = service => { throw new InvalidOperationException("IoC is not initialized"); };
|
||||
public static Action<object> BuildUp = instance => { throw new InvalidOperationException("IoC is not initialized"); };
|
||||
|
||||
public static T Get<T>(string key = null)
|
||||
{
|
||||
return (T)GetInstance(typeof(T), key);
|
||||
}
|
||||
|
||||
public static IEnumerable<T> GetAll<T>()
|
||||
{
|
||||
return GetAllInstances(typeof(T)).Cast<T>();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -54,6 +54,7 @@
|
|||
<SubType>Designer</SubType>
|
||||
</ApplicationDefinition>
|
||||
<Compile Include="Bootstrapper.cs" />
|
||||
<Compile Include="IoC.cs" />
|
||||
<Compile Include="SubView.xaml.cs">
|
||||
<DependentUpon>SubView.xaml</DependentUpon>
|
||||
</Compile>
|
||||
|
|
|
@ -23,7 +23,7 @@ namespace Stylet
|
|||
if (viewType.IsInterface || viewType.IsAbstract || !typeof(UIElement).IsAssignableFrom(viewType))
|
||||
throw new Exception(String.Format("Found type for view : {0}, but it wasn't a class derived from UIElement", viewType.Name));
|
||||
|
||||
var view = (UIElement)Activator.CreateInstance(viewType);
|
||||
var view = (UIElement)IoC.GetInstance(viewType, null);
|
||||
|
||||
// If it doesn't have a code-behind, this won't be called
|
||||
var initializer = viewType.GetMethod("InitializeComponent", BindingFlags.Public | BindingFlags.Instance);
|
||||
|
|
|
@ -10,7 +10,7 @@ namespace Stylet
|
|||
{
|
||||
public interface IWindowManager
|
||||
{
|
||||
|
||||
void ShowWindow(object viewModel, IDictionary<string, object> settings = null);
|
||||
}
|
||||
|
||||
public class WindowManager : IWindowManager
|
||||
|
|
Loading…
Reference in New Issue