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
139 lines (119 loc) · 5.31 KB
/
ViewModelViewHost.cs
File metadata and controls
139 lines (119 loc) · 5.31 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
// 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.Reactive.Linq;
using Splat;
using Xamarin.Forms;
namespace ReactiveUI.XamForms
{
/// <summary>
/// This content view will automatically load and host the view for the given view model. The view model whose view is
/// to be displayed should be assigned to the <see cref="ViewModel"/> property. Optionally, the chosen view can be
/// customized by specifying a contract via <see cref="ViewContractObservable"/> or <see cref="ViewContract"/>.
/// </summary>
public class ViewModelViewHost : ContentView, IViewFor
{
/// <summary>
/// The view model whose associated view is to be displayed.
/// </summary>
public object ViewModel
{
get { return GetValue(ViewModelProperty); }
set { SetValue(ViewModelProperty, value); }
}
/// <summary>
/// Identifies the <see cref="ViewModel"/> property.
/// </summary>
public static readonly BindableProperty ViewModelProperty = BindableProperty.Create(
nameof(ViewModel),
typeof(object),
typeof(ViewModelViewHost),
default(object),
BindingMode.OneWay);
/// <summary>
/// The content to display when <see cref="ViewModel"/> is <see langword="null"/>.
/// </summary>
public View DefaultContent
{
get { return (View)GetValue(DefaultContentProperty); }
set { SetValue(DefaultContentProperty, value); }
}
/// <summary>
/// Identifies the <see cref="DefaultContent"/> property.
/// </summary>
public static readonly BindableProperty DefaultContentProperty = BindableProperty.Create(
nameof(DefaultContent),
typeof(View),
typeof(ViewModelViewHost),
default(View),
BindingMode.OneWay);
/// <summary>
/// The contract to use when resolving the view for the given view model.
/// </summary>
public IObservable<string> ViewContractObservable
{
get { return (IObservable<string>)GetValue(ViewContractObservableProperty); }
set { SetValue(ViewContractObservableProperty, value); }
}
/// <summary>
/// Identifies the <see cref="ViewContractObservable"/> property.
/// </summary>
public static readonly BindableProperty ViewContractObservableProperty = BindableProperty.Create(
nameof(ViewContractObservable),
typeof(IObservable<string>),
typeof(ViewModelViewHost),
Observable<string>.Never,
BindingMode.OneWay);
private string viewContract;
/// <summary>
/// A fixed contract to use when resolving the view for the given view model.
/// </summary>
/// <remarks>
/// This property is a mere convenience so that a fixed contract can be assigned directly in XAML.
/// </remarks>
public string ViewContract
{
get { return this.viewContract; }
set { ViewContractObservable = Observable.Return(value); }
}
/// <summary>
/// Can be used to override the view locator to use when resolving the view. If unspecified, <see cref="ViewLocator.Current"/> will be used.
/// </summary>
public IViewLocator ViewLocator { get; set; }
public ViewModelViewHost()
{
// NB: InUnitTestRunner also returns true in Design Mode
if (ModeDetector.InUnitTestRunner()) {
ViewContractObservable = Observable<string>.Never;
return;
}
ViewContractObservable = Observable<string>.Default;
var vmAndContract = Observable.CombineLatest(
this.WhenAnyValue(x => x.ViewModel),
this.WhenAnyObservable(x => x.ViewContractObservable),
(vm, contract) => new { ViewModel = vm, Contract = contract, });
this.WhenActivated(() => {
return new[] {
vmAndContract.Subscribe(x => {
this.viewContract = x.Contract;
if (x.ViewModel == null) {
this.Content = this.DefaultContent;
return;
}
var viewLocator = ViewLocator ?? ReactiveUI.ViewLocator.Current;
var view = viewLocator.ResolveView(x.ViewModel, x.Contract) ?? viewLocator.ResolveView(x.ViewModel, null);
if (view == null) {
throw new Exception(String.Format("Couldn't find view for '{0}'.", x.ViewModel));
}
var castView = view as View;
if (castView == null) {
throw new Exception(String.Format("View '{0}' is not a subclass of '{1}'.", view.GetType().FullName, typeof(View).FullName));
}
view.ViewModel = x.ViewModel;
this.Content = castView;
})};
});
}
}
}