From 1e90f1eceebd1eeb271a25df90f4d118d31f073a Mon Sep 17 00:00:00 2001 From: Nigel Sampson Date: Tue, 30 Dec 2014 23:42:01 +1300 Subject: [PATCH] LocateForModel falls back to LocateForModelType, workaround for #1 --- src/Caliburn.Micro.Platform/View.cs | 37 ++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/Caliburn.Micro.Platform/View.cs b/src/Caliburn.Micro.Platform/View.cs index 35247203..8ef48078 100644 --- a/src/Caliburn.Micro.Platform/View.cs +++ b/src/Caliburn.Micro.Platform/View.cs @@ -287,9 +287,18 @@ static void OnModelChanged(DependencyObject targetLocation, DependencyPropertyCh if (args.NewValue != null) { var context = GetContext(targetLocation); + var view = ViewLocator.LocateForModel(args.NewValue, targetLocation, context); - SetContentProperty(targetLocation, view); + if (!SetContentProperty(targetLocation, view)) { + + Log.Warn("SetContentProperty failed for ViewLocator.LocateForModel, falling back to LocateForModelType"); + + view = ViewLocator.LocateForModelType(args.NewValue.GetType(), targetLocation, context); + + SetContentProperty(targetLocation, view); + } + ViewModelBinder.Bind(args.NewValue, view, context); } else { @@ -309,30 +318,42 @@ static void OnContextChanged(DependencyObject targetLocation, DependencyProperty var view = ViewLocator.LocateForModel(model, targetLocation, e.NewValue); - SetContentProperty(targetLocation, view); + if (!SetContentProperty(targetLocation, view)) { + + Log.Warn("SetContentProperty failed for ViewLocator.LocateForModel, falling back to LocateForModelType"); + + view = ViewLocator.LocateForModelType(model.GetType(), targetLocation, e.NewValue); + + SetContentProperty(targetLocation, view); + } + ViewModelBinder.Bind(model, view, e.NewValue); } - static void SetContentProperty(object targetLocation, object view) { + static bool SetContentProperty(object targetLocation, object view) { var fe = view as FrameworkElement; if (fe != null && fe.Parent != null) { SetContentPropertyCore(fe.Parent, null); } - SetContentPropertyCore(targetLocation, view); + return SetContentPropertyCore(targetLocation, view); } #if WinRT - static void SetContentPropertyCore(object targetLocation, object view) { + static bool SetContentPropertyCore(object targetLocation, object view) { try { var type = targetLocation.GetType(); var contentPropertyName = GetContentPropertyName(type); type.GetRuntimeProperty(contentPropertyName) .SetValue(targetLocation, view, null); + + return true; } catch (Exception e) { Log.Error(e); + + return false; } } @@ -346,7 +367,7 @@ private static string GetContentPropertyName(Type type) { contentProperty.NamedArguments[0].TypedValue.Value.ToString(); } #else - static void SetContentPropertyCore(object targetLocation, object view) { + static bool SetContentPropertyCore(object targetLocation, object view) { try { var type = targetLocation.GetType(); var contentProperty = type.GetAttributes(true) @@ -354,9 +375,13 @@ static void SetContentPropertyCore(object targetLocation, object view) { type.GetProperty(contentProperty.Name) .SetValue(targetLocation, view, null); + + return true; } catch(Exception e) { Log.Error(e); + + return false; } } #endif