-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix RoutedViewHost and ViewModelViewHost to allow DefaultContent #2816
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
793e713
Refactored RoutedViewHost and ViewModelViewHost to allow DefaultConte…
ChrisPulman d67cd71
Updated to include a ViewModel / Navigation test
ChrisPulman 6466d37
Merge branch 'main' into fix_For#2815
ChrisPulman 75eefbe
Merge branch 'main' into fix_For#2815
ChrisPulman 9c5df4e
Merge branch 'main' into fix_For#2815
ChrisPulman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Copyright (c) 2021 .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. | ||
|
|
||
| using Splat; | ||
|
|
||
| namespace ReactiveUI.Tests | ||
| { | ||
| public class TestView : ReactiveUserControl<TestViewModel>, IScreen | ||
| { | ||
| #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. | ||
|
|
||
| public TestView() | ||
| #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. | ||
| { | ||
| } | ||
|
|
||
| public TestView(IScreen? screen = null) | ||
| { | ||
| Router = screen?.Router ?? Locator.Current.GetService<RoutingState>()!; | ||
| } | ||
|
|
||
| public RoutingState Router { get; } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| // Copyright (c) 2021 .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. | ||
|
|
||
| using System; | ||
| using System.Reactive.Concurrency; | ||
| using System.Windows; | ||
| using DynamicData; | ||
| using ReactiveUI.Tests.Wpf; | ||
| using Splat; | ||
| using Xunit; | ||
|
|
||
| namespace ReactiveUI.Tests | ||
| { | ||
| public class RoutedViewHostTests | ||
| { | ||
| [StaFact] | ||
| public void RoutedViewHostDefaultContentNotNull() | ||
| { | ||
| var uc = new RoutedViewHost | ||
| { | ||
| DefaultContent = new System.Windows.Controls.Label() | ||
| }; | ||
| var window = new WpfTestWindow(); | ||
| window.RootGrid.Children.Add(uc); | ||
|
|
||
| var activation = new ActivationForViewFetcher(); | ||
|
|
||
| activation.GetActivationForView(window).ToObservableChangeSet(scheduler: ImmediateScheduler.Instance).Bind(out var windowActivated).Subscribe(); | ||
|
|
||
| activation.GetActivationForView(uc).ToObservableChangeSet(scheduler: ImmediateScheduler.Instance).Bind(out var controlActivated).Subscribe(); | ||
|
|
||
| var loaded = new RoutedEventArgs | ||
| { | ||
| RoutedEvent = FrameworkElement.LoadedEvent | ||
| }; | ||
|
|
||
| window.RaiseEvent(loaded); | ||
| uc.RaiseEvent(loaded); | ||
|
|
||
| new[] { true }.AssertAreEqual(windowActivated); | ||
| new[] { true }.AssertAreEqual(controlActivated); | ||
|
|
||
| Assert.NotNull(uc.Content); | ||
|
|
||
| window.Dispatcher.InvokeShutdown(); | ||
| } | ||
|
|
||
| [StaFact] | ||
| public void RoutedViewHostDefaultContentNotNullWithViewModelAndActivated() | ||
| { | ||
| Locator.CurrentMutable.Register<RoutingState>(() => new(ImmediateScheduler.Instance)); | ||
| Locator.CurrentMutable.Register<TestViewModel>(() => new()); | ||
| Locator.CurrentMutable.Register<IViewFor<TestViewModel>>(() => new TestView()); | ||
|
|
||
| var uc = new RoutedViewHost | ||
| { | ||
| DefaultContent = new System.Windows.Controls.Label(), | ||
| Router = Locator.Current.GetService<RoutingState>()! | ||
| }; | ||
| var window = new WpfTestWindow(); | ||
| window.RootGrid.Children.Add(uc); | ||
|
|
||
| var activation = new ActivationForViewFetcher(); | ||
|
|
||
| activation.GetActivationForView(window).ToObservableChangeSet(scheduler: ImmediateScheduler.Instance).Bind(out var windowActivated).Subscribe(); | ||
|
|
||
| activation.GetActivationForView(uc).ToObservableChangeSet(scheduler: ImmediateScheduler.Instance).Bind(out var controlActivated).Subscribe(); | ||
|
|
||
| var loaded = new RoutedEventArgs | ||
| { | ||
| RoutedEvent = FrameworkElement.LoadedEvent | ||
| }; | ||
|
|
||
| window.RaiseEvent(loaded); | ||
| uc.RaiseEvent(loaded); | ||
|
|
||
| new[] { true }.AssertAreEqual(windowActivated); | ||
| new[] { true }.AssertAreEqual(controlActivated); | ||
|
|
||
| // Default Content | ||
| Assert.IsType<System.Windows.Controls.Label>(uc.Content); | ||
|
|
||
| // Test Navigation after activated | ||
| uc.Router.Navigate.Execute(Locator.Current.GetService<TestViewModel>()!); | ||
| Assert.IsType<TestView>(uc.Content); | ||
|
|
||
| window.Dispatcher.InvokeShutdown(); | ||
| } | ||
|
|
||
| [StaFact] | ||
| public void RoutedViewHostDefaultContentNotNullWithViewModelAndNotActivated() | ||
| { | ||
| Locator.CurrentMutable.Register<RoutingState>(() => new(ImmediateScheduler.Instance)); | ||
| Locator.CurrentMutable.Register<TestViewModel>(() => new()); | ||
| Locator.CurrentMutable.Register<IViewFor<TestViewModel>>(() => new TestView()); | ||
|
|
||
| var uc = new RoutedViewHost | ||
| { | ||
| DefaultContent = new System.Windows.Controls.Label(), | ||
| Router = Locator.Current.GetService<RoutingState>()! | ||
| }; | ||
| var window = new WpfTestWindow(); | ||
| window.RootGrid.Children.Add(uc); | ||
|
|
||
| var activation = new ActivationForViewFetcher(); | ||
|
|
||
| activation.GetActivationForView(window).ToObservableChangeSet(scheduler: ImmediateScheduler.Instance).Bind(out var windowActivated).Subscribe(); | ||
|
|
||
| activation.GetActivationForView(uc).ToObservableChangeSet(scheduler: ImmediateScheduler.Instance).Bind(out var controlActivated).Subscribe(); | ||
|
|
||
| var loaded = new RoutedEventArgs | ||
| { | ||
| RoutedEvent = FrameworkElement.LoadedEvent | ||
| }; | ||
|
|
||
| // Test navigation before Activation. | ||
| uc.Router.Navigate.Execute(Locator.Current.GetService<TestViewModel>()!); | ||
|
|
||
| // Activate | ||
| window.RaiseEvent(loaded); | ||
| uc.RaiseEvent(loaded); | ||
|
|
||
| new[] { true }.AssertAreEqual(windowActivated); | ||
| new[] { true }.AssertAreEqual(controlActivated); | ||
|
|
||
| // Test Navigation before activated | ||
| Assert.IsType<TestView>(uc.Content); | ||
|
|
||
| window.Dispatcher.InvokeShutdown(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Copyright (c) 2021 .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. | ||
|
|
||
| using System; | ||
| using System.Reactive.Concurrency; | ||
| using System.Windows; | ||
| using DynamicData; | ||
| using ReactiveUI.Tests.Wpf; | ||
| using Splat; | ||
| using Xunit; | ||
|
|
||
| namespace ReactiveUI.Tests | ||
| { | ||
| public class ViewModelViewHostTests | ||
| { | ||
| [StaFact] | ||
| public void ViewModelViewHostDefaultContentNotNull() | ||
| { | ||
| var uc = new ViewModelViewHost | ||
| { | ||
| DefaultContent = new System.Windows.Controls.Label() | ||
| }; | ||
| var window = new WpfTestWindow(); | ||
| window.RootGrid.Children.Add(uc); | ||
|
|
||
| var activation = new ActivationForViewFetcher(); | ||
|
|
||
| activation.GetActivationForView(window) | ||
| .ToObservableChangeSet(scheduler: ImmediateScheduler.Instance) | ||
| .Bind(out var windowActivated) | ||
| .Subscribe(); | ||
|
|
||
| activation.GetActivationForView(uc) | ||
| .ToObservableChangeSet(scheduler: ImmediateScheduler.Instance) | ||
| .Bind(out var controlActivated) | ||
| .Subscribe(); | ||
|
|
||
| var loaded = new RoutedEventArgs | ||
| { | ||
| RoutedEvent = FrameworkElement.LoadedEvent | ||
| }; | ||
|
|
||
| window.RaiseEvent(loaded); | ||
| uc.RaiseEvent(loaded); | ||
|
|
||
| new[] { true }.AssertAreEqual(windowActivated); | ||
| new[] { true }.AssertAreEqual(controlActivated); | ||
|
|
||
| Assert.NotNull(uc.Content); | ||
|
|
||
| window.Dispatcher.InvokeShutdown(); | ||
| } | ||
|
|
||
| [StaFact] | ||
| public void ViewModelViewHostContentNotNullWithViewModelAndActivated() | ||
| { | ||
| Locator.CurrentMutable.Register<TestViewModel>(() => new()); | ||
| Locator.CurrentMutable.Register<IViewFor<TestViewModel>>(() => new TestView()); | ||
|
|
||
| var uc = new ViewModelViewHost | ||
| { | ||
| DefaultContent = new System.Windows.Controls.Label(), | ||
| ViewModel = Locator.Current.GetService<TestViewModel>() | ||
| }; | ||
| var window = new WpfTestWindow(); | ||
| window.RootGrid.Children.Add(uc); | ||
|
|
||
| var activation = new ActivationForViewFetcher(); | ||
|
|
||
| activation.GetActivationForView(window).ToObservableChangeSet(scheduler: ImmediateScheduler.Instance).Bind(out var windowActivated).Subscribe(); | ||
|
|
||
| activation.GetActivationForView(uc).ToObservableChangeSet(scheduler: ImmediateScheduler.Instance).Bind(out var controlActivated).Subscribe(); | ||
|
|
||
| var loaded = new RoutedEventArgs | ||
| { | ||
| RoutedEvent = FrameworkElement.LoadedEvent | ||
| }; | ||
|
|
||
| window.RaiseEvent(loaded); | ||
| uc.RaiseEvent(loaded); | ||
|
|
||
| new[] { true }.AssertAreEqual(windowActivated); | ||
| new[] { true }.AssertAreEqual(controlActivated); | ||
|
|
||
| // Test IViewFor<ViewModel> after activated | ||
| Assert.IsType<TestView>(uc.Content); | ||
|
|
||
| window.Dispatcher.InvokeShutdown(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.