1
1
using System . Reactive . Disposables ;
2
- using System . Windows ;
3
2
using ReactiveUI ;
4
3
5
4
namespace ReactiveDemo
6
5
{
7
- public partial class MainWindow : IViewFor < AppViewModel >
6
+ // MainWindow class derives off ReactiveWindow which implements the IViewFor<TViewModel>
7
+ // interface using a WPF DependencyProperty. We need this to use WhenActivated extension
8
+ // method that helps us handling View and ViewModel activation and deactivation.
9
+ public partial class MainWindow : ReactiveWindow < AppViewModel >
8
10
{
9
- // Using a DependencyProperty as the backing store for ViewModel.
10
- // This enables animation, styling, binding, etc.
11
- public static readonly DependencyProperty ViewModelProperty =
12
- DependencyProperty . Register ( "ViewModel" ,
13
- typeof ( AppViewModel ) , typeof ( MainWindow ) ,
14
- new PropertyMetadata ( null ) ) ;
15
-
16
11
public MainWindow ( )
17
12
{
18
13
InitializeComponent ( ) ;
19
14
ViewModel = new AppViewModel ( ) ;
20
15
21
- // We create our bindings here. These are the code behind bindings which allow
16
+ // We create our bindings here. These are the code behind bindings which allow
22
17
// type safety. The bindings will only become active when the Window is being shown.
23
- // We register our subscription in our disposableRegistration, this will cause
18
+ // We register our subscription in our disposableRegistration, this will cause
24
19
// the binding subscription to become inactive when the Window is closed.
25
- // The disposableRegistration is a CompositeDisposable which is a container of
26
- // other Disposables. We use the DisposeWith() extension method which simply adds
20
+ // The disposableRegistration is a CompositeDisposable which is a container of
21
+ // other Disposables. We use the DisposeWith() extension method which simply adds
27
22
// the subscription disposable to the CompositeDisposable.
28
23
this . WhenActivated ( disposableRegistration =>
29
24
{
@@ -45,21 +40,5 @@ public MainWindow()
45
40
. DisposeWith ( disposableRegistration ) ;
46
41
} ) ;
47
42
}
48
-
49
- // Our main view model instance.
50
- public AppViewModel ViewModel
51
- {
52
- get => ( AppViewModel ) GetValue ( ViewModelProperty ) ;
53
- set => SetValue ( ViewModelProperty , value ) ;
54
- }
55
-
56
- // This is required by the interface IViewFor, you always just set it to use the
57
- // main ViewModel property. Note on XAML based platforms we have a control called
58
- // ReactiveUserControl that abstracts this.
59
- object IViewFor . ViewModel
60
- {
61
- get => ViewModel ;
62
- set => ViewModel = ( AppViewModel ) value ;
63
- }
64
43
}
65
44
}
0 commit comments