mirror of https://github.com/AMT-Cheif/Stylet.git
Document ViewManager a bit
This commit is contained in:
parent
b634b301c9
commit
0d3538c932
|
@ -38,7 +38,7 @@ namespace Stylet.Samples.OverridingViewManager
|
|||
this.viewModelToViewMapping = mappings.ToDictionary(x => x.ViewModel, x => x.View);
|
||||
}
|
||||
|
||||
public override UIElement LocateViewForModel(object model)
|
||||
public override UIElement CreateViewForModel(object model)
|
||||
{
|
||||
Type viewType;
|
||||
if (!this.viewModelToViewMapping.TryGetValue(model.GetType(), out viewType))
|
||||
|
|
|
@ -10,10 +10,27 @@ using System.Windows;
|
|||
|
||||
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
|
||||
{
|
||||
/// <summary>
|
||||
/// Called by the View.Model attached property when the ViewModel its bound to changes
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -34,7 +51,7 @@ namespace Stylet
|
|||
}
|
||||
else
|
||||
{
|
||||
view = this.LocateViewForModel(e.NewValue);
|
||||
view = this.CreateViewForModel(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 viewName = Regex.Replace(modelName, @"ViewModel", "View");
|
||||
|
|
|
@ -32,7 +32,7 @@ namespace Stylet
|
|||
{
|
||||
var viewManager = IoC.Get<IViewManager>();
|
||||
|
||||
var view = viewManager.LocateViewForModel(viewModel);
|
||||
var view = viewManager.CreateViewForModel(viewModel);
|
||||
var window = view as Window;
|
||||
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));
|
||||
|
|
|
@ -29,7 +29,7 @@ namespace StyletUnitTests
|
|||
public void ShowWindowAsksViewManagerForView()
|
||||
{
|
||||
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
|
||||
try { this.windowManager.ShowWindow(model); }
|
||||
catch (Exception) { }
|
||||
|
@ -40,7 +40,7 @@ namespace StyletUnitTests
|
|||
public void ShowWindowThrowsIfViewIsntAWindow()
|
||||
{
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue