-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathViewModelViewHost.cs
223 lines (192 loc) · 8.16 KB
/
ViewModelViewHost.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// 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 view model control host which will find and host the View for a ViewModel.
/// </summary>
[DefaultProperty("ViewModel")]
public partial class ViewModelControlHost : UserControl, IReactiveObject, IViewFor
{
private readonly CompositeDisposable _disposables = [];
#pragma warning disable IDE0032 // Use auto property
private Control? _defaultContent;
private IObservable<string>? _viewContractObservable;
private object? _viewModel;
private object? _content;
#pragma warning restore IDE0032 // Use auto property
private bool _cacheViews;
/// <summary>
/// Initializes a new instance of the <see cref="ViewModelControlHost"/> class.
/// </summary>
public ViewModelControlHost()
{
InitializeComponent();
_cacheViews = DefaultCacheViewsEnabled;
SetupBindings().ForEach(_disposables.Add);
}
/// <inheritdoc/>
public event PropertyChangingEventHandler? PropertyChanging;
/// <inheritdoc/>
public event PropertyChangedEventHandler? PropertyChanged;
/// <summary>
/// Gets or sets a value indicating whether [default cache views enabled].
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public static bool DefaultCacheViewsEnabled { get; set; }
/// <summary>
/// Gets the current view.
/// </summary>
public Control? CurrentView => _content as Control;
/// <summary>
/// Gets or sets the default content.
/// </summary>
[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 view contract observable.
/// </summary>
/// <value>
/// The view contract observable.
/// </value>
[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/>
[Category("ReactiveUI")]
[Description("The viewmodel to host.")]
[Bindable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public object? ViewModel
{
get => _viewModel;
set => this.RaiseAndSetIfChanged(ref _viewModel, value);
}
/// <summary>
/// Gets or sets the content.
/// </summary>
[Category("ReactiveUI")]
[Description("The Current View")]
[Bindable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public object? Content
{
get => _content;
protected set => this.RaiseAndSetIfChanged(ref _content, value);
}
/// <summary>
/// Gets or sets a value indicating whether to cache views.
/// </summary>
[Category("ReactiveUI")]
[Description("Cache Views")]
[Bindable(true)]
[DefaultValue(true)]
public bool CacheViews
{
get => _cacheViews;
set => this.RaiseAndSetIfChanged(ref _cacheViews, value);
}
/// <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 IEnumerable<IDisposable> SetupBindings()
{
var viewChanges =
this.WhenAnyValue(x => x!.Content)
.WhereNotNull()
.OfType<Control>()
.Subscribe(x =>
{
// change the view in the ui
SuspendLayout();
// clear out existing visible control view
foreach (Control? c in Controls)
{
c?.Dispose();
Controls.Remove(c);
}
x!.Dock = DockStyle.Fill;
Controls.Add(x);
ResumeLayout();
});
yield return viewChanges!;
yield return this.WhenAnyValue(x => x.DefaultContent).Subscribe(x =>
{
if (x is not null)
{
Content = DefaultContent;
}
});
ViewContractObservable = Observable.Return(string.Empty);
var vmAndContract =
this.WhenAnyValue(x => x.ViewModel)
.CombineLatest(
this.WhenAnyObservable(x => x.ViewContractObservable!),
(vm, contract) => new { ViewModel = vm, Contract = contract });
yield return vmAndContract.Subscribe(
x =>
{
// set content to default when viewmodel is null
if (ViewModel is null)
{
if (DefaultContent is not null)
{
Content = DefaultContent;
}
return;
}
if (CacheViews)
{
// when caching views, check the current viewmodel and type
var c = _content as IViewFor;
if (c?.ViewModel is not null && c.ViewModel.GetType() == x.ViewModel!.GetType())
{
c.ViewModel = x.ViewModel;
// return early here after setting the viewmodel
// allowing the view to update it's bindings
return;
}
}
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;
}
},
RxApp.DefaultExceptionHandler!.OnNext);
}
}