From e222af3c84ea724c104df1a60013db785d1b704f Mon Sep 17 00:00:00 2001 From: Antony Male Date: Tue, 24 Mar 2015 09:23:35 +0000 Subject: [PATCH] Throw if the user tries to show a Window using s:View.Model Fixes #5 --- Stylet/ViewManager.cs | 22 ++++++++++++++++++++++ Stylet/WindowManager.cs | 5 +++-- StyletUnitTests/ViewManagerTests.cs | 13 +++++++++++++ StyletUnitTests/WindowManagerTests.cs | 2 +- 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/Stylet/ViewManager.cs b/Stylet/ViewManager.cs index 1f62f23..720ff50 100644 --- a/Stylet/ViewManager.cs +++ b/Stylet/ViewManager.cs @@ -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; } } + + /// + /// Exception raise when the located View is of the wrong type (Window when expected UserControl, etc) + /// + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2237:MarkISerializableTypesWithSerializable")] + public class StyletInvalidViewTypeException : Exception + { + /// + /// Initialises a new instance of the class + /// + /// Message associated with the Exception + public StyletInvalidViewTypeException(string message) + : base(message) + { } + } } diff --git a/Stylet/WindowManager.cs b/Stylet/WindowManager.cs index 8fa62de..b23e079 100644 --- a/Stylet/WindowManager.cs +++ b/Stylet/WindowManager.cs @@ -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; } diff --git a/StyletUnitTests/ViewManagerTests.cs b/StyletUnitTests/ViewManagerTests.cs index 07e6207..129480f 100644 --- a/StyletUnitTests/ViewManagerTests.cs +++ b/StyletUnitTests/ViewManagerTests.cs @@ -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(() => viewManager.OnModelChanged(target, null, model)); + } + [Test] public void CreateViewForModelReturnsNullIfViewNotFound() { diff --git a/StyletUnitTests/WindowManagerTests.cs b/StyletUnitTests/WindowManagerTests.cs index 846a076..08217f3 100644 --- a/StyletUnitTests/WindowManagerTests.cs +++ b/StyletUnitTests/WindowManagerTests.cs @@ -88,7 +88,7 @@ namespace StyletUnitTests { var model = new object(); this.viewManager.Setup(x => x.CreateAndBindViewForModelIfNecessary(model)).Returns(new UIElement()); - Assert.Throws(() => this.windowManager.CreateWindow(model, false)); + Assert.Throws(() => this.windowManager.CreateWindow(model, false)); } [Test]