diff --git a/src/ReactiveUI.Tests/API/ApiApprovalTests.ReactiveUI.net461.approved.txt b/src/ReactiveUI.Tests/API/ApiApprovalTests.ReactiveUI.net461.approved.txt index bfe25a65b4..c317e942fa 100644 --- a/src/ReactiveUI.Tests/API/ApiApprovalTests.ReactiveUI.net461.approved.txt +++ b/src/ReactiveUI.Tests/API/ApiApprovalTests.ReactiveUI.net461.approved.txt @@ -116,6 +116,12 @@ namespace ReactiveUI public System.IDisposable BindCommandToObject(System.Windows.Input.ICommand command, object target, System.IObservable commandParameter, string eventName) { } public int GetAffinityForObject(System.Type type, bool hasEventTarget) { } } + public sealed class DefaultViewLocator : ReactiveUI.IViewLocator, Splat.IEnableLogger + { + public System.Func ViewModelToViewFunc { get; set; } + public ReactiveUI.IViewFor ResolveView(T viewModel, string contract = null) + where T : class { } + } public class static DependencyResolverMixins { public static void InitializeReactiveUI(this Splat.IMutableDependencyResolver resolver) { } diff --git a/src/ReactiveUI.Tests/API/ApiApprovalTests.ReactiveUI.netcoreapp3.0.approved.txt b/src/ReactiveUI.Tests/API/ApiApprovalTests.ReactiveUI.netcoreapp3.0.approved.txt index 66b38aaa29..187b0de0a9 100644 --- a/src/ReactiveUI.Tests/API/ApiApprovalTests.ReactiveUI.netcoreapp3.0.approved.txt +++ b/src/ReactiveUI.Tests/API/ApiApprovalTests.ReactiveUI.netcoreapp3.0.approved.txt @@ -116,6 +116,12 @@ namespace ReactiveUI public System.IDisposable BindCommandToObject(System.Windows.Input.ICommand command, object target, System.IObservable commandParameter, string eventName) { } public int GetAffinityForObject(System.Type type, bool hasEventTarget) { } } + public sealed class DefaultViewLocator : ReactiveUI.IViewLocator, Splat.IEnableLogger + { + public System.Func ViewModelToViewFunc { get; set; } + public ReactiveUI.IViewFor ResolveView(T viewModel, string contract = null) + where T : class { } + } public class static DependencyResolverMixins { public static void InitializeReactiveUI(this Splat.IMutableDependencyResolver resolver) { } diff --git a/src/ReactiveUI.Tests/Locator/DefaultViewLocatorTests.cs b/src/ReactiveUI.Tests/Locator/DefaultViewLocatorTests.cs index 8ab74f8468..ea9f9475b2 100644 --- a/src/ReactiveUI.Tests/Locator/DefaultViewLocatorTests.cs +++ b/src/ReactiveUI.Tests/Locator/DefaultViewLocatorTests.cs @@ -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(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(vm); + Assert.IsType(result); + } + } } } diff --git a/src/ReactiveUI.Tests/Locator/Mocks/RoutableFooCustomView.cs b/src/ReactiveUI.Tests/Locator/Mocks/RoutableFooCustomView.cs new file mode 100644 index 0000000000..8191172c32 --- /dev/null +++ b/src/ReactiveUI.Tests/Locator/Mocks/RoutableFooCustomView.cs @@ -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 + { + object IViewFor.ViewModel + { + get => ViewModel; + set => ViewModel = (IRoutableFooViewModel)value; + } + + public IRoutableFooViewModel ViewModel { get; set; } + } +} diff --git a/src/ReactiveUI.Tests/Platforms/winforms/CommandBindingTests.cs b/src/ReactiveUI.Tests/Platforms/winforms/CommandBindingTests.cs index f7b011deaf..7792536db9 100644 --- a/src/ReactiveUI.Tests/Platforms/winforms/CommandBindingTests.cs +++ b/src/ReactiveUI.Tests/Platforms/winforms/CommandBindingTests.cs @@ -66,7 +66,7 @@ public void CommandBinderBindsToCustomControl() } } - [Fact] + [Fact(Skip = "https://github.com/reactiveui/ReactiveUI/issues/2279")] public void CommandBinderBindsToCustomComponent() { var fixture = new CreatesWinformsCommandBinding(); diff --git a/src/ReactiveUI/View/DefaultViewLocator.cs b/src/ReactiveUI/View/DefaultViewLocator.cs index d2a8d9c4a1..002df76964 100644 --- a/src/ReactiveUI/View/DefaultViewLocator.cs +++ b/src/ReactiveUI/View/DefaultViewLocator.cs @@ -11,14 +11,18 @@ namespace ReactiveUI { - internal sealed class DefaultViewLocator : IViewLocator, IEnableLogger + /// + /// Default implementation for . The default + /// behavior is to replace instances of "View" with "ViewMode" in the Fully Qualified Name of the ViewModel type. + /// + public sealed class DefaultViewLocator : IViewLocator, IEnableLogger { /// /// Initializes a new instance of the class. /// /// The method which will convert a ViewModel name into a View. [SuppressMessage("Globalization", "CA1307: operator could change based on locale settings", Justification = "Replace() does not have third parameter on all platforms")] - public DefaultViewLocator(Func viewModelToViewFunc = null) + internal DefaultViewLocator(Func viewModelToViewFunc = null) { ViewModelToViewFunc = viewModelToViewFunc ?? (vm => vm.Replace("ViewModel", "View")); }