-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Allow Developers Bypass the Default Fallback Behavior (resolves #…
…3713) (#3718) This changes applys to the Maui/Wpf/XamarinForm platform. <!-- Please be sure to read the [Contribute](https://github.com/reactiveui/reactiveui#contribute) section of the README --> **What kind of change does this PR introduce?** <!-- Bug fix, feature, docs update, ... --> - Feature Request. See #3713 **What is the current behavior?** <!-- You can also link to an open issue here. --> 1. The ViewModelViewHost resolves view by the ViewContract property. Currently ignores the `ViewContract` condition if nothing found. **What is the new behavior?** 1. Add a property of `ContractFallbackByPass` so that we can bypass this fallback behavior. 2. Expose a virtual method , i.e. `protected virtual void ResolveViewForViewModel(object? viewModel, string? contract)` , which allows developers override this behavior. **What might this PR break?** As far as I can see, it does not break anying. **Please check if the PR fulfills these requirements** - [x] Tests for the changes have been added (for bug fixes / features) - [X] Docs have been added / updated (for bug fixes / features) **Other information**: For WPF/MAUI/XamForms/WinUI, the `ContractFallbackByPass` is set to false by default. So it won't breaking existing apps. However, I find the [current WinForms implementation](https://github.com/reactiveui/ReactiveUI/blob/9c36b0f0701ee7005556ccafaeb503a96ff6b75f/src/ReactiveUI.Winforms/ViewModelViewHost.cs#L210-L211) has no default fallback behaivor as same as WPF ```c# var viewLocator = ViewLocator ?? ReactiveUI.ViewLocator.Current; var view = viewLocator.ResolveView(x.ViewModel, x.Contract); if (view is not null) { view.ViewModel = x.ViewModel; Content = view; } ``` So I didn't add such a property for WinForms. Is it better to add such a property that is set to true by default ? --------- Co-authored-by: Chris Pulman <[email protected]>
- Loading branch information
1 parent
e28392d
commit 632b33d
Showing
11 changed files
with
370 additions
and
46 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
85 changes: 85 additions & 0 deletions
85
src/ReactiveUI.Tests/Platforms/wpf/Mocks/ViewModelViewHosts/FakeViewWithContract.cs
This file contains 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,85 @@ | ||
// Copyright (c) 2023 .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.Windows; | ||
using System.Windows.Controls; | ||
|
||
namespace ReactiveUI.Tests.Wpf; | ||
|
||
public static class FakeViewWithContract | ||
{ | ||
internal const string ContractA = "ContractA"; | ||
internal const string ContractB = "ContractB"; | ||
|
||
public class MyViewModel : ReactiveObject | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Used as the default view with no contracted. | ||
/// </summary> | ||
public class View0 : UserControl, IViewFor<MyViewModel> | ||
{ | ||
|
||
// Using a DependencyProperty as the backing store for ViewModel. This enables animation, styling, binding, etc... | ||
public static readonly DependencyProperty ViewModelProperty = | ||
DependencyProperty.Register("ViewModel", typeof(MyViewModel), typeof(View0), new PropertyMetadata(null)); | ||
|
||
/// <summary> | ||
/// Gets or sets the ViewModel. | ||
/// </summary> | ||
public MyViewModel? ViewModel | ||
{ | ||
get { return (MyViewModel)GetValue(ViewModelProperty); } | ||
set { SetValue(ViewModelProperty, value); } | ||
} | ||
|
||
object? IViewFor.ViewModel { get => ViewModel; set => ViewModel = (MyViewModel?)value; } | ||
} | ||
|
||
/// <summary> | ||
/// the view with ContractA. | ||
/// </summary> | ||
public class ViewA : UserControl, IViewFor<MyViewModel> | ||
{ | ||
|
||
// Using a DependencyProperty as the backing store for ViewModel. This enables animation, styling, binding, etc... | ||
public static readonly DependencyProperty ViewModelProperty = | ||
DependencyProperty.Register("ViewModel", typeof(MyViewModel), typeof(ViewA), new PropertyMetadata(null)); | ||
|
||
/// <summary> | ||
/// Gets or sets the ViewModel. | ||
/// </summary> | ||
public MyViewModel? ViewModel | ||
{ | ||
get { return (MyViewModel)GetValue(ViewModelProperty); } | ||
set { SetValue(ViewModelProperty, value); } | ||
} | ||
|
||
object? IViewFor.ViewModel { get => ViewModel; set => ViewModel = (MyViewModel?)value; } | ||
} | ||
|
||
/// <summary> | ||
/// the view as ContractB. | ||
/// </summary> | ||
public class ViewB : UserControl, IViewFor<MyViewModel> | ||
{ | ||
|
||
// Using a DependencyProperty as the backing store for ViewModel. This enables animation, styling, binding, etc... | ||
public static readonly DependencyProperty ViewModelProperty = | ||
DependencyProperty.Register("ViewModel", typeof(MyViewModel), typeof(ViewB), new PropertyMetadata(null)); | ||
|
||
/// <summary> | ||
/// Gets or sets the ViewModel. | ||
/// </summary> | ||
public MyViewModel? ViewModel | ||
{ | ||
get { return (MyViewModel)GetValue(ViewModelProperty); } | ||
set { SetValue(ViewModelProperty, value); } | ||
} | ||
|
||
object? IViewFor.ViewModel { get => ViewModel; set => ViewModel = (MyViewModel?)value; } | ||
} | ||
} |
Oops, something went wrong.