Throw if the user tries to show a Window using s:View.Model

Fixes #5
This commit is contained in:
Antony Male 2015-03-24 09:23:35 +00:00
parent 9b27dcf3de
commit e222af3c84
4 changed files with 39 additions and 3 deletions

View File

@ -105,6 +105,13 @@ namespace Stylet
{
logger.Info("View.Model changed for {0} from {1} to {2}", targetLocation, oldValue, newValue);
var view = this.CreateAndBindViewForModelIfNecessary(newValue);
if (view is Window)
{
var e = new StyletInvalidViewTypeException(String.Format("s:View.Model=\"...\" tried to show a View of type '{0}', but that View derives from the Window class. " +
"Make sure any Views you display using s:View.Model=\"...\" do not derive from Window (use UserControl or similar)", view.GetType().Name));
logger.Error(e);
throw e;
}
View.SetContentProperty(targetLocation, view);
}
else
@ -273,4 +280,19 @@ namespace Stylet
this.ViewTypeName = viewTypeName;
}
}
/// <summary>
/// Exception raise when the located View is of the wrong type (Window when expected UserControl, etc)
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2237:MarkISerializableTypesWithSerializable")]
public class StyletInvalidViewTypeException : Exception
{
/// <summary>
/// Initialises a new instance of the <see cref="StyletInvalidViewException"/> class
/// </summary>
/// <param name="message">Message associated with the Exception</param>
public StyletInvalidViewTypeException(string message)
: base(message)
{ }
}
}

View File

@ -124,8 +124,9 @@ namespace Stylet
var window = view as Window;
if (window == null)
{
var e = new ArgumentException(String.Format("WindowManager.ShowWindow or .ShowDialog tried to show a View of type '{0}', but that View doesn't derive from the Window class. " +
"Make sure any Views you display derive from Window (not UserControl, etc)", view == null ? "(null)" : view.GetType().Name));
var e = new StyletInvalidViewTypeException(String.Format("WindowManager.ShowWindow or .ShowDialog tried to show a View of type '{0}', but that View doesn't derive from the Window class. " +
"Make sure any Views you display using WindowManager.ShowWindow or .ShowDialog derive from Window (not UserControl, etc)",
view == null ? "(null)" : view.GetType().Name));
logger.Error(e);
throw e;
}

View File

@ -178,6 +178,19 @@ namespace StyletUnitTests
Assert.AreEqual(view, target.Content);
}
[Test]
public void OnModelChangedThrowsIfViewIsAWindow()
{
var target = new ContentControl();
var model = new object();
var view = new Window();
var viewManager = new CreatingAndBindingViewManager(this.viewManagerConfig.Object);
viewManager.View = view;
Assert.Throws<StyletInvalidViewTypeException>(() => viewManager.OnModelChanged(target, null, model));
}
[Test]
public void CreateViewForModelReturnsNullIfViewNotFound()
{

View File

@ -88,7 +88,7 @@ namespace StyletUnitTests
{
var model = new object();
this.viewManager.Setup(x => x.CreateAndBindViewForModelIfNecessary(model)).Returns(new UIElement());
Assert.Throws<ArgumentException>(() => this.windowManager.CreateWindow(model, false));
Assert.Throws<StyletInvalidViewTypeException>(() => this.windowManager.CreateWindow(model, false));
}
[Test]