Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/ReactiveUI.Tests/ViewLocatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public class FooViewModel : ReactiveObject, IFooViewModel
{
}

public class FooViewModelWithWeirdName : ReactiveObject, IFooViewModel
{
}

public class BarViewModel : ReactiveObject, IBarViewModel
{
}
Expand Down Expand Up @@ -88,6 +92,24 @@ public void ByDefaultViewModelIsReplacedWithViewWhenDeterminingTheServiceName()
}
}

[Fact]
public void TheRuntimeTypeOfTheViewModelIsUsedToResolveTheView()
{
var resolver = new ModernDependencyResolver();

resolver.InitializeSplat();
resolver.InitializeReactiveUI();
resolver.Register(() => new FooView(), typeof(FooView));

using (resolver.WithResolver()) {
var fixture = new DefaultViewLocator();
object vm = new FooViewModel();

var result = fixture.ResolveView(vm);
Assert.True(result is FooView);
}
}

[Fact]
public void ViewModelToViewNamingConventionCanBeCustomized()
{
Expand Down Expand Up @@ -172,7 +194,7 @@ public void CanResolveViewFromViewModelInterfaceUsingClassRegistration()

using (resolver.WithResolver()) {
var fixture = new DefaultViewLocator();
IFooViewModel vm = new FooViewModel();
IFooViewModel vm = new FooViewModelWithWeirdName();

var result = fixture.ResolveView(vm);
Assert.True(result is FooView);
Expand Down
23 changes: 20 additions & 3 deletions src/ReactiveUI/ViewLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,22 @@ public DefaultViewLocator(Func<string, string> viewModelToViewFunc = null)
/// </summary>
/// <remarks>
/// <para>
/// Given view model type <c>T</c>, this implementation will attempt to resolve the following views:
/// Given view model type <c>T</c> with runtime type <c>RT</c>, this implementation will attempt to resolve the following views:
/// <list type="number">
/// <item>
/// <description>
/// Look for a service registered under the type whose name is given to us by <see cref="ViewModelToViewFunc"/> (which defaults to changing "ViewModel" to "View").
/// Look for a service registered under the type whose name is given to us by passing <c>RT</c> to <see cref="ViewModelToViewFunc"/> (which defaults to changing "ViewModel" to "View").
/// </description>
/// </item>
/// <item>
/// <description>
/// Look for a service registered under the type <c>IViewFor&lt;RT&gt;</c>.
/// </description>
/// </item>
/// <item>
/// <item>
/// <description>
/// Look for a service registered under the type whose name is given to us by passing <c>T</c> to <see cref="ViewModelToViewFunc"/> (which defaults to changing "ViewModel" to "View").
/// </description>
/// </item>
/// <item>
Expand Down Expand Up @@ -82,7 +93,13 @@ public DefaultViewLocator(Func<string, string> viewModelToViewFunc = null)
public IViewFor ResolveView<T>(T viewModel, string contract = null)
where T : class
{
var view = this.AttemptViewResolutionFor(typeof(T), contract);
var view = this.AttemptViewResolutionFor(viewModel.GetType(), contract);

if (view != null) {
return view;
}

view = this.AttemptViewResolutionFor(typeof(T), contract);

if (view != null) {
return view;
Expand Down