Document ViewManager a bit

This commit is contained in:
Antony Male 2014-04-10 13:11:47 +01:00
parent b634b301c9
commit 0d3538c932
4 changed files with 24 additions and 7 deletions

View File

@ -38,7 +38,7 @@ namespace Stylet.Samples.OverridingViewManager
this.viewModelToViewMapping = mappings.ToDictionary(x => x.ViewModel, x => x.View); this.viewModelToViewMapping = mappings.ToDictionary(x => x.ViewModel, x => x.View);
} }
public override UIElement LocateViewForModel(object model) public override UIElement CreateViewForModel(object model)
{ {
Type viewType; Type viewType;
if (!this.viewModelToViewMapping.TryGetValue(model.GetType(), out viewType)) if (!this.viewModelToViewMapping.TryGetValue(model.GetType(), out viewType))

View File

@ -10,10 +10,27 @@ using System.Windows;
namespace Stylet namespace Stylet
{ {
/// <summary>
/// Responsible for managing views. Locates the correct view, instantiates it, attaches it to its ViewModel correctly, and handles the View.Model attached property
/// </summary>
public interface IViewManager public interface IViewManager
{ {
/// <summary>
/// Called by the View.Model attached property when the ViewModel its bound to changes
void OnModelChanged(DependencyObject targetLocation, DependencyPropertyChangedEventArgs e); void OnModelChanged(DependencyObject targetLocation, DependencyPropertyChangedEventArgs e);
UIElement LocateViewForModel(object model);
/// <summary>
/// Given an instance of a ViewModel, locate the correct view for it, and instantiate it
/// </summary>
/// <param name="model">ViewModel to locate the view for</param>
/// <returns>An instance of the correct view</returns>
UIElement CreateViewForModel(object model);
/// <summary>
/// Given an instance of a ViewModel and an instance of its View, bind the two together
/// </summary>
/// <param name="view">View to bind to the ViewModel</param>
/// <param name="viewModel">ViewModel to bind the View to</param>
void BindViewToModel(UIElement view, object viewModel); void BindViewToModel(UIElement view, object viewModel);
} }
@ -34,7 +51,7 @@ namespace Stylet
} }
else else
{ {
view = this.LocateViewForModel(e.NewValue); view = this.CreateViewForModel(e.NewValue);
this.BindViewToModel(view, e.NewValue); this.BindViewToModel(view, e.NewValue);
} }
@ -46,7 +63,7 @@ namespace Stylet
} }
} }
public virtual UIElement LocateViewForModel(object model) public virtual UIElement CreateViewForModel(object model)
{ {
var modelName = model.GetType().FullName; var modelName = model.GetType().FullName;
var viewName = Regex.Replace(modelName, @"ViewModel", "View"); var viewName = Regex.Replace(modelName, @"ViewModel", "View");

View File

@ -32,7 +32,7 @@ namespace Stylet
{ {
var viewManager = IoC.Get<IViewManager>(); var viewManager = IoC.Get<IViewManager>();
var view = viewManager.LocateViewForModel(viewModel); var view = viewManager.CreateViewForModel(viewModel);
var window = view as Window; var window = view as Window;
if (window == null) if (window == null)
throw new Exception(String.Format("Tried to show {0} as a window, but it isn't a Window", view == null ? "(null)" : view.GetType().Name)); throw new Exception(String.Format("Tried to show {0} as a window, but it isn't a Window", view == null ? "(null)" : view.GetType().Name));

View File

@ -29,7 +29,7 @@ namespace StyletUnitTests
public void ShowWindowAsksViewManagerForView() public void ShowWindowAsksViewManagerForView()
{ {
var model = new object(); var model = new object();
this.viewManager.Setup(x => x.LocateViewForModel(model)).Verifiable(); this.viewManager.Setup(x => x.CreateViewForModel(model)).Verifiable();
// Don't care if this throws - that's OK // Don't care if this throws - that's OK
try { this.windowManager.ShowWindow(model); } try { this.windowManager.ShowWindow(model); }
catch (Exception) { } catch (Exception) { }
@ -40,7 +40,7 @@ namespace StyletUnitTests
public void ShowWindowThrowsIfViewIsntAWindow() public void ShowWindowThrowsIfViewIsntAWindow()
{ {
var model = new object(); var model = new object();
this.viewManager.Setup(x => x.LocateViewForModel(model)).Returns(new UIElement()); this.viewManager.Setup(x => x.CreateViewForModel(model)).Returns(new UIElement());
Assert.Throws<Exception>(() => this.windowManager.ShowWindow(model)); Assert.Throws<Exception>(() => this.windowManager.ShowWindow(model));
} }
} }