-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathRoutedViewHost.cs
165 lines (139 loc) · 6.37 KB
/
RoutedViewHost.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright (c) 2024 .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.Forms;
namespace ReactiveUI.Winforms;
/// <summary>
/// A control host which will handling routing between different ViewModels and Views.
/// </summary>
[DefaultProperty("ViewModel")]
public partial class RoutedControlHost : UserControl, IReactiveObject
{
private readonly CompositeDisposable _disposables = [];
#pragma warning disable IDE0032 // Use auto property
private RoutingState? _router;
private Control? _defaultContent;
private IObservable<string>? _viewContractObservable;
#pragma warning restore IDE0032 // Use auto property
/// <summary>
/// Initializes a new instance of the <see cref="RoutedControlHost"/> class.
/// </summary>
public RoutedControlHost()
{
InitializeComponent();
_disposables.Add(this.WhenAny(x => x.DefaultContent, x => x.Value).Subscribe(x =>
{
if (x is not null && Controls.Count == 0)
{
Controls.Add(InitView(x));
components?.Add(DefaultContent);
}
}));
ViewContractObservable = Observable<string>.Default;
var vmAndContract =
this.WhenAnyObservable(x => x.Router!.CurrentViewModel!)
.CombineLatest(
this.WhenAnyObservable(x => x.ViewContractObservable!),
(vm, contract) => new { ViewModel = vm, Contract = contract });
Control? viewLastAdded = null;
_disposables.Add(vmAndContract.Subscribe(
x =>
{
// clear all hosted controls (view or default content)
SuspendLayout();
Controls.Clear();
viewLastAdded?.Dispose();
if (x.ViewModel is null)
{
if (DefaultContent is not null)
{
InitView(DefaultContent);
Controls.Add(DefaultContent);
}
ResumeLayout();
return;
}
var viewLocator = ViewLocator ?? ReactiveUI.ViewLocator.Current;
var view = viewLocator.ResolveView(x.ViewModel, x.Contract);
if (view is not null)
{
view.ViewModel = x.ViewModel;
viewLastAdded = InitView((Control)view);
}
if (viewLastAdded is not null)
{
Controls.Add(viewLastAdded);
}
ResumeLayout();
},
RxApp.DefaultExceptionHandler!.OnNext));
}
/// <inheritdoc/>
public event PropertyChangingEventHandler? PropertyChanging;
/// <inheritdoc/>
public event PropertyChangedEventHandler? PropertyChanged;
/// <summary>
/// Gets or sets the default content.
/// </summary>
/// <value>
/// The default content.
/// </value>
[Category("ReactiveUI")]
[Description("The default control when no viewmodel is specified")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public Control? DefaultContent
{
get => _defaultContent;
set => this.RaiseAndSetIfChanged(ref _defaultContent, value);
}
/// <summary>
/// Gets or sets the <see cref="RoutingState"/> of the view model stack.
/// </summary>
[Category("ReactiveUI")]
[Description("The router.")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public RoutingState? Router
{
get => _router;
set => this.RaiseAndSetIfChanged(ref _router, value);
}
/// <summary>
/// Gets or sets the view contract observable.
/// </summary>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public IObservable<string>? ViewContractObservable
{
get => _viewContractObservable;
set => this.RaiseAndSetIfChanged(ref _viewContractObservable, value);
}
/// <summary>
/// Gets or sets the view locator.
/// </summary>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public IViewLocator? ViewLocator { get; set; }
/// <inheritdoc/>
void IReactiveObject.RaisePropertyChanging(PropertyChangingEventArgs args) => PropertyChanging?.Invoke(this, args);
/// <inheritdoc/>
void IReactiveObject.RaisePropertyChanged(PropertyChangedEventArgs args) => PropertyChanged?.Invoke(this, args);
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && components is not null)
{
components.Dispose();
_disposables.Dispose();
}
base.Dispose(disposing);
}
private static Control InitView(Control view)
{
view.Dock = DockStyle.Fill;
return view;
}
}