forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewModelViewHost.cs
More file actions
188 lines (152 loc) · 6.38 KB
/
ViewModelViewHost.cs
File metadata and controls
188 lines (152 loc) · 6.38 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MS-PL license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
using ReactiveUI;
using Splat;
namespace ReactiveUI.Winforms
{
[DefaultProperty("ViewModel")]
public partial class ViewModelControlHost : UserControl, IReactiveObject, IViewFor
{
readonly CompositeDisposable disposables = new CompositeDisposable();
Control defaultContent;
IObservable<string> viewContractObservable;
object viewModel;
public static bool DefaultCacheViewsEnabled { get; set; }
public ViewModelControlHost()
{
this.InitializeComponent();
this.cacheViews = DefaultCacheViewsEnabled;
foreach (var subscription in this.setupBindings()) {
disposables.Add(subscription);
}
}
IEnumerable<IDisposable> setupBindings()
{
var viewChanges =
this.WhenAnyValue(x => x.Content).Select(x => x as Control).Where(x => x != null).Subscribe(x => {
// change the view in the ui
this.SuspendLayout();
// clear out existing visible control view
foreach (Control c in this.Controls) {
c.Dispose();
this.Controls.Remove(c);
}
x.Dock = DockStyle.Fill;
this.Controls.Add(x);
this.ResumeLayout();
});
yield return viewChanges;
yield return this.WhenAny(x => x.DefaultContent, x => x.Value).Subscribe(x => {
if (x != null) {
this.Content = DefaultContent;
}
});
this.ViewContractObservable = Observable<string>.Default;
var vmAndContract =
this.WhenAny(x => x.ViewModel, x => x.Value)
.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 (this.ViewModel == null) {
if (this.DefaultContent != null) {
this.Content = DefaultContent;
}
return;
}
if (CacheViews) {
// when caching views, check the current viewmodel and type
var c = content as IViewFor;
if (c != null && c.ViewModel != 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;
}
}
IViewLocator viewLocator = this.ViewLocator ?? ReactiveUI.ViewLocator.Current;
IViewFor view = viewLocator.ResolveView(x.ViewModel, x.Contract);
this.Content = view;
view.ViewModel = x.ViewModel;
}, RxApp.DefaultExceptionHandler.OnNext);
}
public event PropertyChangingEventHandler PropertyChanging {
add { PropertyChangingEventManager.AddHandler(this, value); }
remove { PropertyChangingEventManager.RemoveHandler(this, value); }
}
void IReactiveObject.RaisePropertyChanging(PropertyChangingEventArgs args)
{
PropertyChangingEventManager.DeliverEvent(this, args);
}
public event PropertyChangedEventHandler PropertyChanged {
add { PropertyChangedEventManager.AddHandler(this, value); }
remove { PropertyChangedEventManager.RemoveHandler(this, value); }
}
void IReactiveObject.RaisePropertyChanged(PropertyChangedEventArgs args)
{
PropertyChangedEventManager.DeliverEvent(this, args);
}
public Control CurrentView {
get { return this.content as Control; }
}
[Category("ReactiveUI")]
[Description("The default control when no viewmodel is specified")]
public Control DefaultContent {
get { return this.defaultContent; }
set { this.RaiseAndSetIfChanged(ref this.defaultContent, value); }
}
[Browsable(false)]
public IObservable<string> ViewContractObservable {
get { return this.viewContractObservable; }
set { this.RaiseAndSetIfChanged(ref this.viewContractObservable, value); }
}
[Browsable(false)]
public IViewLocator ViewLocator { get; set; }
[Category("ReactiveUI")]
[Description("The viewmodel to host.")]
[Bindable(true)]
public object ViewModel {
get { return this.viewModel; }
set { this.RaiseAndSetIfChanged(ref this.viewModel, value); }
}
object content;
[Category("ReactiveUI")]
[Description("The Current View")]
[Bindable(true)]
public object Content {
get { return this.content; }
protected set { this.RaiseAndSetIfChanged(ref this.content, value); }
}
bool cacheViews;
[Category("ReactiveUI")]
[Description("Cache Views")]
[Bindable(true)]
[DefaultValue(true)]
public bool CacheViews {
get { return this.cacheViews; }
set { this.RaiseAndSetIfChanged(ref this.cacheViews, value); }
}
/// <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 && (this.components != null))
{
this.components.Dispose();
this.disposables.Dispose();
}
base.Dispose(disposing);
}
}
}