Skip to content

Commit 11bb11f

Browse files
author
Paul Betts
committed
Merge pull request #335 from reactiveui/view-locator-fallback
Fall back to non orientation-specific views
2 parents 0afa770 + e9f8930 commit 11bb11f

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

ReactiveUI.Platforms/Cocoa/RoutedViewHost.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public RoutedViewHost(NSView targetView)
6767
}
6868

6969
var viewLocator = ViewLocator ?? ReactiveUI.ViewLocator.Current;
70-
var view = viewLocator.ResolveView(x.ViewModel, x.Contract);
70+
var view = viewLocator.ResolveView(x.ViewModel, x.Contract) ?? viewLocator.ResolveView(x.ViewModel, null);
7171
view.ViewModel = x.ViewModel;
7272

7373
if (view is NSViewController) {

ReactiveUI.Platforms/Xaml/RoutedViewHost.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ public RoutedViewHost()
8989
}
9090

9191
var viewLocator = ViewLocator ?? ReactiveUI.ViewLocator.Current;
92-
var view = viewLocator.ResolveView(x.Item1, x.Item2);
92+
var view = viewLocator.ResolveView(x.Item1, x.Item2) ?? viewLocator.ResolveView(x.Item1, null);
93+
9394
view.ViewModel = x.Item1;
9495
Content = view;
9596
}, ex => RxApp.DefaultExceptionHandler.OnNext(ex));

ReactiveUI/ViewLocator.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static IViewLocator Current {
2121
}
2222
}
2323

24-
class DefaultViewLocator : IViewLocator
24+
class DefaultViewLocator : IViewLocator, IEnableLogger
2525
{
2626
public Func<string, string> ViewModelToViewFunc { get; set; }
2727

@@ -56,21 +56,29 @@ public IViewFor ResolveView<T>(T viewModel, string contract = null)
5656

5757
// IFooBarView that implements IViewFor (or custom ViewModelToViewFunc)
5858
var typeToFind = ViewModelToViewFunc(viewModel.GetType().AssemblyQualifiedName);
59-
try {
60-
var type = Reflection.ReallyFindType(typeToFind, false);
59+
60+
var ret = attemptToResolveView(Reflection.ReallyFindType(typeToFind, false), contract);
61+
if (ret != null) return ret;
6162

62-
if (type != null) {
63-
var ret = RxApp.DependencyResolver.GetService(type, contract) as IViewFor;
64-
if (ret != null) return ret;
65-
}
63+
// IViewFor<FooBarViewModel> (the original behavior in RxUI 3.1)
64+
var viewType = typeof (IViewFor<>);
65+
return attemptToResolveView(viewType.MakeGenericType(viewModel.GetType()), contract);
66+
}
67+
68+
IViewFor attemptToResolveView(Type type, string contract)
69+
{
70+
if (type == null) return null;
71+
72+
var ret = default(IViewFor);
73+
74+
try {
75+
ret = (IViewFor)RxApp.DependencyResolver.GetService(type, contract);
6676
} catch (Exception ex) {
67-
LogHost.Default.DebugException("Couldn't instantiate " + typeToFind, ex);
77+
this.Log().ErrorException("Failed to instantiate view: " + type.FullName, ex);
78+
throw;
6879
}
6980

70-
var viewType = typeof (IViewFor<>);
71-
72-
// IViewFor<FooBarViewModel> (the original behavior in RxUI 3.1)
73-
return (IViewFor) RxApp.DependencyResolver.GetService(viewType.MakeGenericType(viewModel.GetType()), contract);
81+
return ret;
7482
}
7583

7684
static string interfaceifyTypeName(string typeName)

0 commit comments

Comments
 (0)