@@ -23,7 +23,7 @@ namespace ReactiveUI.Xaml
23
23
/// the View and wire up the ViewModel whenever a new ViewModel is
24
24
/// navigated to. Put this control as the only control in your Window.
25
25
/// </summary>
26
- public class RoutedViewHost : TransitioningContentControl
26
+ public class RoutedViewHost : TransitioningContentControl , IViewFor
27
27
{
28
28
IDisposable _inner = null ;
29
29
@@ -57,6 +57,9 @@ public IObservable<string> ViewContractObservable {
57
57
58
58
public IViewLocator ViewLocator { get ; set ; }
59
59
60
+ // NB: This is just a scam to get WhenActivated
61
+ object IViewFor . ViewModel { get ; set ; }
62
+
60
63
public RoutedViewHost ( )
61
64
{
62
65
HorizontalContentAlignment = HorizontalAlignment . Stretch ;
@@ -83,25 +86,27 @@ public RoutedViewHost()
83
86
this . WhenAnyObservable ( x => x . ViewContractObservable ) ,
84
87
( vm , contract ) => Tuple . Create ( vm , contract ) ) ;
85
88
86
- // NB: The DistinctUntilChanged is useful because most views in
87
- // WinRT will end up getting here twice - once for configuring
88
- // the RoutedViewHost's ViewModel, and once on load via SizeChanged
89
- vmAndContract . DistinctUntilChanged ( ) . Subscribe ( x => {
90
- if ( x . Item1 == null ) {
91
- Content = DefaultContent ;
92
- return ;
93
- }
94
-
95
- var viewLocator = ViewLocator ?? ReactiveUI . ViewLocator . Current ;
96
- var view = viewLocator . ResolveView ( x . Item1 , x . Item2 ) ?? viewLocator . ResolveView ( x . Item1 , null ) ;
97
-
98
- if ( view == null ) {
99
- throw new Exception ( String . Format ( "Couldn't find view for '{0}'." , x . Item1 ) ) ;
100
- }
101
-
102
- view . ViewModel = x . Item1 ;
103
- Content = view ;
104
- } , ex => RxApp . DefaultExceptionHandler . OnNext ( ex ) ) ;
89
+ this . WhenActivated ( d => {
90
+ // NB: The DistinctUntilChanged is useful because most views in
91
+ // WinRT will end up getting here twice - once for configuring
92
+ // the RoutedViewHost's ViewModel, and once on load via SizeChanged
93
+ d ( vmAndContract . DistinctUntilChanged ( ) . Subscribe ( x => {
94
+ if ( x . Item1 == null ) {
95
+ Content = DefaultContent ;
96
+ return ;
97
+ }
98
+
99
+ var viewLocator = ViewLocator ?? ReactiveUI . ViewLocator . Current ;
100
+ var view = viewLocator . ResolveView ( x . Item1 , x . Item2 ) ?? viewLocator . ResolveView ( x . Item1 , null ) ;
101
+
102
+ if ( view == null ) {
103
+ throw new Exception ( String . Format ( "Couldn't find view for '{0}'." , x . Item1 ) ) ;
104
+ }
105
+
106
+ view . ViewModel = x . Item1 ;
107
+ Content = view ;
108
+ } , ex => RxApp . DefaultExceptionHandler . OnNext ( ex ) ) ) ;
109
+ } ) ;
105
110
}
106
111
}
107
112
}
0 commit comments