@@ -68,26 +68,40 @@ public virtual void ScrollIntoView(NavigationContext item)
6868 protected IView ? ResolveView ( NavigationContext context )
6969 {
7070 var view = context . View ;
71+ INavigationAware ? navigationAware = null ;
72+
7173 if ( view is null )
7274 {
7375 view = context . ServiceProvider . GetRequiredKeyedService < IView > ( context . ViewName ) ;
74- var navigationAware = context . ServiceProvider . GetRequiredKeyedService < INavigationAware > ( context . ViewName ) ;
76+ navigationAware = context . ServiceProvider . GetRequiredKeyedService < INavigationAware > ( context . ViewName ) ;
77+
78+ view . DataContext = navigationAware ;
7579
76- if ( Current . TryTakeData ( out var previousData ) )
80+ if ( navigationAware is ICanUnload canUnloadNavigationAware )
7781 {
78- previousData . NavigationAware . OnNavigatedFrom ( context ) ;
82+ canUnloadNavigationAware . RequestUnload += ( ) =>
83+ {
84+ DeActivate ( context ) ;
85+ } ;
7986 }
8087
81- view . DataContext = navigationAware ;
82- navigationAware . OnNavigatedTo ( context ) ;
83- navigationAware . RequestUnload += ( ) =>
84- {
85- DeActivate ( context ) ;
86- } ;
87- Current . SetData ( ( view , navigationAware ) ) ;
8888 context . View = view ;
8989 ViewCache . AddOrUpdate ( context , view , ( key , value ) => view ) ;
9090 }
91+ else
92+ {
93+ navigationAware = view . DataContext as INavigationAware
94+ ?? context . ServiceProvider . GetRequiredKeyedService < INavigationAware > ( context . ViewName ) ;
95+ }
96+ if ( Current . TryTakeData ( out var previousData ) )
97+ {
98+ previousData . NavigationAware . OnNavigatedFrom ( context ) ;
99+ }
100+
101+ navigationAware . OnNavigatedTo ( context ) ;
102+
103+ Current . SetData ( ( view , navigationAware ) ) ;
104+ context . Alias = navigationAware . Alias ;
91105 return view ;
92106 }
93107
@@ -124,27 +138,7 @@ private IDataTemplate CreateRegionDataTemplate()
124138 {
125139 return null ;
126140 }
127- var view = context . View ;
128- if ( view is null )
129- {
130- view = context . ServiceProvider . GetRequiredKeyedService < IView > ( context . ViewName ) ;
131- var navigationAware = context . ServiceProvider . GetRequiredKeyedService < INavigationAware > ( context . ViewName ) ;
132-
133- if ( Current . TryTakeData ( out var previousData ) )
134- {
135- previousData . NavigationAware . OnNavigatedFrom ( context ) ;
136- }
137-
138- view . DataContext = navigationAware ;
139- navigationAware . OnNavigatedTo ( context ) ;
140- navigationAware . RequestUnload += ( ) =>
141- {
142- DeActivate ( context ) ;
143- } ;
144- Current . SetData ( ( view , navigationAware ) ) ;
145- context . View = view ;
146- ViewCache . AddOrUpdate ( context , view , ( key , value ) => view ) ;
147- }
141+ var view = ResolveView ( context ) ;
148142 return view as Control ;
149143 } ) ;
150144 }
0 commit comments