ViewManager no longer depends on IoC

This commit is contained in:
Antony Male 2014-11-30 14:05:08 +00:00
parent 254e2f8a9b
commit e36179098e
3 changed files with 26 additions and 14 deletions

View File

@ -1,6 +1,7 @@
using StyletIoC;
using System;
using System.Collections.Generic;
using System.Windows;
namespace Stylet
{
@ -37,9 +38,9 @@ namespace Stylet
protected virtual void DefaultConfigureIoC(StyletIoCBuilder builder)
{
// Mark these as auto-bindings, so the user can replace them if they want
builder.Bind<IViewManager>().ToInstance(new ViewManager(type => this.Container.Get(type) as UIElement)).AsWeakBinding();
builder.Bind<IWindowManager>().To<WindowManager>().InSingletonScope().AsWeakBinding();
builder.Bind<IEventAggregator>().To<EventAggregator>().InSingletonScope().AsWeakBinding();
builder.Bind<IViewManager>().To<ViewManager>().InSingletonScope().AsWeakBinding();
builder.Bind<IMessageBoxViewModel>().To<MessageBoxViewModel>().AsWeakBinding();
builder.Autobind(AssemblySource.Assemblies);

View File

@ -34,7 +34,13 @@ namespace Stylet
/// </summary>
public class ViewManager : IViewManager
{
private static ILogger logger = LogManager.GetLogger(typeof(ViewManager));
private static readonly ILogger logger = LogManager.GetLogger(typeof(ViewManager));
private readonly Func<Type, UIElement> viewFactory;
public ViewManager(Func<Type, UIElement> viewFactory)
{
this.viewFactory = viewFactory;
}
/// <summary>
/// Called by View whenever its current View.Model changes. Will locate and instantiate the correct view, and set it as the target's Content
@ -135,7 +141,7 @@ namespace Stylet
throw e;
}
var view = (UIElement)IoC.GetInstance(viewType, null);
var view = this.viewFactory(viewType);
// If it doesn't have a code-behind, this won't be called
var initializer = viewType.GetMethod("InitializeComponent", BindingFlags.Public | BindingFlags.Instance);

View File

@ -30,6 +30,8 @@ namespace StyletUnitTests
private class AccessibleViewManager : ViewManager
{
public AccessibleViewManager() : base(null) { }
public new UIElement CreateViewForModel(object model)
{
return base.CreateViewForModel(model);
@ -45,6 +47,9 @@ namespace StyletUnitTests
{
public UIElement View;
public object RequestedModel;
public CreatingAndBindingViewManager() : base(null) { }
protected override UIElement CreateViewForModel(object model)
{
this.RequestedModel = model;
@ -62,6 +67,8 @@ namespace StyletUnitTests
private class LocatingViewManager : ViewManager
{
public LocatingViewManager(Func<Type, UIElement> viewFactory) : base(viewFactory) { }
public Type LocatedViewType;
protected override Type LocateViewForModel(Type modelType)
{
@ -81,6 +88,8 @@ namespace StyletUnitTests
private class MyViewManager : ViewManager
{
public MyViewManager() : base(null) { }
public new Type LocateViewForModel(Type modelType)
{
return base.LocateViewForModel(modelType);
@ -166,7 +175,7 @@ namespace StyletUnitTests
[Test]
public void CreateViewForModelThrowsIfViewIsNotConcreteUIElement()
{
var viewManager = new LocatingViewManager();
var viewManager = new LocatingViewManager(null);
viewManager.LocatedViewType = typeof(I1);
Assert.Throws<StyletViewLocationException>(() => viewManager.CreateAndBindViewForModel(new object()));
@ -182,13 +191,11 @@ namespace StyletUnitTests
public void CreateViewForModelCallsFetchesViewAndCallsInitializeComponent()
{
var view = new TestView();
IoC.GetInstance = (t, k) =>
var viewManager = new LocatingViewManager(type =>
{
Assert.AreEqual(typeof(TestView), t);
Assert.Null(k);
Assert.AreEqual(typeof(TestView), type);
return view;
};
var viewManager = new LocatingViewManager();
});
viewManager.LocatedViewType = typeof(TestView);
var returnedView = viewManager.CreateAndBindViewForModel(new object());
@ -201,13 +208,11 @@ namespace StyletUnitTests
public void CreateViewForModelDoesNotComplainIfNoInitializeComponentMethod()
{
var view = new UIElement();
IoC.GetInstance = (t, k) =>
var viewManager = new LocatingViewManager(type =>
{
Assert.AreEqual(typeof(UIElement), t);
Assert.Null(k);
Assert.AreEqual(typeof(UIElement), type);
return view;
};
var viewManager = new LocatingViewManager();
});
viewManager.LocatedViewType = typeof(UIElement);
var returnedView = viewManager.CreateAndBindViewForModel(new object());