11using System . Reactive . Disposables ;
2- using System . Windows ;
32using ReactiveUI ;
43
54namespace ReactiveDemo
65{
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 >
810 {
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-
1611 public MainWindow ( )
1712 {
1813 InitializeComponent ( ) ;
1914 ViewModel = new AppViewModel ( ) ;
2015
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
2217 // 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
2419 // 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
2722 // the subscription disposable to the CompositeDisposable.
2823 this . WhenActivated ( disposableRegistration =>
2924 {
@@ -45,21 +40,5 @@ public MainWindow()
4540 . DisposeWith ( disposableRegistration ) ;
4641 } ) ;
4742 }
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- }
6443 }
6544}
0 commit comments