Skip to content
Permalink
Browse files

feature: DefaultViewLocator now public for users to override ViewMode…

…lToViewFunc (#2275)
  • Loading branch information
fdoyon authored and glennawatson committed Dec 15, 2019
1 parent 0dd8f2c commit 858ef01a9f8e37feb45cd5193276dfb9f46bcafa
@@ -116,6 +116,12 @@ namespace ReactiveUI
public System.IDisposable BindCommandToObject<TEventArgs>(System.Windows.Input.ICommand command, object target, System.IObservable<object> commandParameter, string eventName) { }
public int GetAffinityForObject(System.Type type, bool hasEventTarget) { }
}
public sealed class DefaultViewLocator : ReactiveUI.IViewLocator, Splat.IEnableLogger
{
public System.Func<string, string> ViewModelToViewFunc { get; set; }
public ReactiveUI.IViewFor ResolveView<T>(T viewModel, string contract = null)
where T : class { }
}
public class static DependencyResolverMixins
{
public static void InitializeReactiveUI(this Splat.IMutableDependencyResolver resolver) { }
@@ -116,6 +116,12 @@ namespace ReactiveUI
public System.IDisposable BindCommandToObject<TEventArgs>(System.Windows.Input.ICommand command, object target, System.IObservable<object> commandParameter, string eventName) { }
public int GetAffinityForObject(System.Type type, bool hasEventTarget) { }
}
public sealed class DefaultViewLocator : ReactiveUI.IViewLocator, Splat.IEnableLogger
{
public System.Func<string, string> ViewModelToViewFunc { get; set; }
public ReactiveUI.IViewFor ResolveView<T>(T viewModel, string contract = null)
where T : class { }
}
public class static DependencyResolverMixins
{
public static void InitializeReactiveUI(this Splat.IMutableDependencyResolver resolver) { }
@@ -61,7 +61,8 @@ public void ViewModelToViewNamingConventionCanBeCustomized()
using (resolver.WithResolver())
{
var fixture = new DefaultViewLocator();
fixture.ViewModelToViewFunc = viewModelName => viewModelName.Replace("ViewModel", "WithWeirdConvention");
fixture.ViewModelToViewFunc =
viewModelName => viewModelName.Replace("ViewModel", "WithWeirdConvention");
var vm = new FooViewModel();

var result = fixture.ResolveView(vm);
@@ -220,7 +221,8 @@ public void NoErrorIsRaisedIfATypeCannotBeFound()
using (resolver.WithResolver())
{
var fixture = new DefaultViewLocator();
fixture.ViewModelToViewFunc = viewModelName => "DoesNotExist, " + typeof(DefaultViewLocatorTests).Assembly.FullName;
fixture.ViewModelToViewFunc = viewModelName =>
"DoesNotExist, " + typeof(DefaultViewLocatorTests).Assembly.FullName;
var vm = new FooViewModel();

var result = fixture.ResolveView(vm);
@@ -320,5 +322,25 @@ public void CanResolveViewFromViewModelWithIRoutableViewModelType()
Assert.IsType<RoutableFooView>(result);
}
}

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

resolver.InitializeSplat();
resolver.InitializeReactiveUI();
resolver.Register(() => new RoutableFooCustomView());

using (resolver.WithResolver())
{
var fixture = new DefaultViewLocator();
fixture.ViewModelToViewFunc = x => x.Replace("ViewModel", "CustomView");
var vm = new RoutableFooViewModel();

var result = fixture.ResolveView<IRoutableViewModel>(vm);
Assert.IsType<RoutableFooCustomView>(result);
}
}
}
}
@@ -0,0 +1,18 @@
// Copyright (c) 2019 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

namespace ReactiveUI.Tests
{
public class RoutableFooCustomView : IViewFor<IRoutableFooViewModel>
{
object IViewFor.ViewModel
{
get => ViewModel;
set => ViewModel = (IRoutableFooViewModel)value;
}

public IRoutableFooViewModel ViewModel { get; set; }
}
}
@@ -66,7 +66,7 @@ public void CommandBinderBindsToCustomControl()
}
}

[Fact]
[Fact(Skip = "https://github.com/reactiveui/ReactiveUI/issues/2279")]
public void CommandBinderBindsToCustomComponent()
{
var fixture = new CreatesWinformsCommandBinding();
@@ -11,14 +11,18 @@

namespace ReactiveUI
{
internal sealed class DefaultViewLocator : IViewLocator, IEnableLogger
/// <summary>
/// Default implementation for <see cref="IViewLocator"/>. The default <see cref="ViewModelToViewFunc"/>
/// behavior is to replace instances of "View" with "ViewMode" in the Fully Qualified Name of the ViewModel type.
/// </summary>
public sealed class DefaultViewLocator : IViewLocator, IEnableLogger
{
/// <summary>
/// Initializes a new instance of the <see cref="DefaultViewLocator"/> class.
/// </summary>
/// <param name="viewModelToViewFunc">The method which will convert a ViewModel name into a View.</param>
[SuppressMessage("Globalization", "CA1307: operator could change based on locale settings", Justification = "Replace() does not have third parameter on all platforms")]
public DefaultViewLocator(Func<string, string> viewModelToViewFunc = null)
internal DefaultViewLocator(Func<string, string> viewModelToViewFunc = null)
{
ViewModelToViewFunc = viewModelToViewFunc ?? (vm => vm.Replace("ViewModel", "View"));
}

0 comments on commit 858ef01

Please sign in to comment.
You can’t perform that action at this time.