Skip to content

Commit

Permalink
LocateForModel falls back to LocateForModelType, workaround for #1
Browse files Browse the repository at this point in the history
  • Loading branch information
nigel-sampson committed Dec 30, 2014
1 parent 40104f6 commit 1e90f1e
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions src/Caliburn.Micro.Platform/View.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
}

Expand All @@ -346,17 +367,21 @@ 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<ContentPropertyAttribute>(true)
.FirstOrDefault() ?? DefaultContentProperty;

type.GetProperty(contentProperty.Name)
.SetValue(targetLocation, view, null);

return true;
}
catch(Exception e) {
Log.Error(e);

return false;
}
}
#endif
Expand Down

0 comments on commit 1e90f1e

Please sign in to comment.