Eliminate View.ViewManager

I think that's the last step towards supporting multiple Applications
(if anyone every actually wants to do that). It's good architecture,
anyway
This commit is contained in:
Antony Male 2015-06-02 15:35:19 +01:00
parent 9f3a7d665a
commit 97192899eb
4 changed files with 18 additions and 24 deletions

View File

@ -85,7 +85,9 @@ namespace Stylet
this.ConfigureBootstrapper();
View.ViewManager = (IViewManager)this.GetInstance(typeof(IViewManager));
// Cater for the unit tests, which can't sensibly stub Application
if (this.Application != null)
this.Application.Resources.Add(View.ViewManagerResourceKey, this.GetInstance(typeof(IViewManager)));
this.Configure();
this.Launch();

View File

@ -11,6 +11,11 @@ namespace Stylet.Xaml
/// </summary>
public class View : DependencyObject
{
/// <summary>
/// Key which will be used to retrieve the ViewManager associated with the current application, from application's resources
/// </summary>
public const string ViewManagerResourceKey = "b9a38199-8cb3-4103-8526-c6cfcd089df7";
/// <summary>
/// Initial value of the ActionTarget property.
/// This can be used as a marker - if the property has this value, it hasn't yet been assigned to anything else.
@ -19,11 +24,6 @@ namespace Stylet.Xaml
private static readonly ContentPropertyAttribute defaultContentProperty = new ContentPropertyAttribute("Content");
/// <summary>
/// Gets or sets the <see cref="IViewManager"/> to be used. This should be set by the Bootstrapper.
/// </summary>
public static IViewManager ViewManager { get; set; }
/// <summary>
/// Get the ActionTarget associated with the given object
/// </summary>
@ -78,7 +78,9 @@ namespace Stylet.Xaml
public static readonly DependencyProperty ModelProperty =
DependencyProperty.RegisterAttached("Model", typeof(object), typeof(View), new PropertyMetadata(defaultModelValue, (d, e) =>
{
if (ViewManager == null)
var viewManager = ((FrameworkElement)d).Resources[ViewManagerResourceKey] as IViewManager;
if (viewManager == null)
{
if (Execute.InDesignMode)
{
@ -94,14 +96,14 @@ namespace Stylet.Xaml
}
else
{
throw new InvalidOperationException("View.ViewManager is unassigned. This should have been set by the Bootstrapper");
throw new InvalidOperationException("The ViewManager resource is unassigned. This should have been set by the Bootstrapper");
}
}
else
{
// It appears we can be reset to the default value on destruction
var newValue = e.NewValue == defaultModelValue ? null : e.NewValue;
ViewManager.OnModelChanged(d, e.OldValue, newValue);
viewManager.OnModelChanged(d, e.OldValue, newValue);
}
}));

View File

@ -129,13 +129,6 @@ namespace StyletUnitTests
Assert.True(this.bootstrapper.ConfigureCalled);
}
[Test]
public void StartAssignsViewManager()
{
this.bootstrapper.Start(new string[0]);
Assert.AreEqual(View.ViewManager, this.viewManager.Object);
}
[Test]
public void StartAssignsArgs()
{

View File

@ -35,7 +35,6 @@ namespace StyletUnitTests
public void SetUp()
{
this.viewManager = new Mock<IViewManager>();
View.ViewManager = this.viewManager.Object;
}
[TearDown]
@ -55,7 +54,8 @@ namespace StyletUnitTests
[Test]
public void ModelStores()
{
var obj = new DependencyObject();
var obj = new FrameworkElement();
obj.Resources.Add(View.ViewManagerResourceKey, this.viewManager.Object);
View.SetModel(obj, 5);
Assert.AreEqual(5, View.GetModel(obj));
}
@ -63,7 +63,8 @@ namespace StyletUnitTests
[Test]
public void ChangingModelCallsOnModelChanged()
{
var obj = new DependencyObject();
var obj = new FrameworkElement();
obj.Resources.Add(View.ViewManagerResourceKey, this.viewManager.Object);
var model = new object();
View.SetModel(obj, null);
@ -95,8 +96,7 @@ namespace StyletUnitTests
[Test]
public void SettingModelThrowsExceptionIfViewManagerNotSet()
{
View.ViewManager = null;
var view = new UIElement();
var view = new FrameworkElement();
Assert.Throws<InvalidOperationException>(() => View.SetModel(view, new object()));
}
@ -104,7 +104,6 @@ namespace StyletUnitTests
public void InDesignModeSettingViewModelWithBrokenBindingGivesAppropriateMessage()
{
Execute.InDesignMode = true;
View.ViewManager = null;
var element = new ContentControl();
// Don't set View.Model to a binding - just a random object
@ -120,7 +119,6 @@ namespace StyletUnitTests
public void InDesignModeSettingViewModelWithCollectionBindingGivesAppropriateMessage()
{
Execute.InDesignMode = true;
View.ViewManager = null;
var element = new ContentControl();
var vm = new TestViewModel();
@ -139,7 +137,6 @@ namespace StyletUnitTests
public void InDesignModeSettingViewModelWithGoodBindingGivesAppropriateMessage()
{
Execute.InDesignMode = true;
View.ViewManager = null;
var element = new ContentControl();
var vm = new TestViewModel();