Split out ViewManager.LocateViewForModel, and fix a sample

This commit is contained in:
Antony Male 2014-04-10 20:35:32 +01:00
parent ea8cbd41f7
commit 5884592c07
4 changed files with 15 additions and 8 deletions

View File

@ -1,4 +1,5 @@
using System;
using StyletIoC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -8,11 +9,11 @@ namespace Stylet.Samples.OverridingViewManager
{
public class Bootstrapper : Bootstrapper<ShellViewModel>
{
protected override void ConfigureIoC(StyletIoC.IStyletIoCBuilder builder)
protected override void ConfigureIoC(IStyletIoCBuilder builder)
{
base.ConfigureIoC(builder);
builder.Bind<IViewManager>().To<CustomViewManager>();
builder.Bind<IViewManager>().To<CustomViewManager>().InSingletonScope();
}
}
}

Binary file not shown.

View File

@ -42,7 +42,7 @@ namespace Stylet
public interface IConductor<T>
{
/// <summary>
/// Active the given item
/// Activate the given item
/// </summary>
/// <param name="item">Item to activate</param>
void ActivateItem(T item);

View File

@ -63,18 +63,24 @@ namespace Stylet
}
}
public virtual UIElement CreateViewForModel(object model)
public virtual Type LocalViewForModel(Type modelType)
{
var modelName = model.GetType().FullName;
var viewName = Regex.Replace(modelName, @"ViewModel", "View");
var viewName = Regex.Replace(modelType.FullName, @"ViewModel", "View");
// TODO: This might need some more thinking
var viewType = AssemblySource.Assemblies.SelectMany(x => x.GetExportedTypes()).FirstOrDefault(x => x.FullName == viewName);
if (viewType == null)
throw new Exception(String.Format("Unable to find a View with type {0}", viewName));
return viewType;
}
public virtual UIElement CreateViewForModel(object model)
{
var viewType = this.LocalViewForModel(model.GetType());
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));
throw new Exception(String.Format("Found type for view: {0}, but it wasn't a class derived from UIElement", viewType.Name));
var view = (UIElement)IoC.GetInstance(viewType, null);