Skip to content

Commit c0340da

Browse files
authored
Merge pull request #59 from NeverMorewd/bugfix-0728
#58
2 parents bbef140 + 8522280 commit c0340da

File tree

1 file changed

+20
-33
lines changed
  • src/Lemon.ModuleNavigation.Avaloniaui/Regions

1 file changed

+20
-33
lines changed

src/Lemon.ModuleNavigation.Avaloniaui/Regions/Region.cs

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -68,29 +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);
75-
76-
if (Current.TryTakeData(out var previousData))
77-
{
78-
previousData.NavigationAware.OnNavigatedFrom(context);
79-
}
76+
navigationAware = context.ServiceProvider.GetRequiredKeyedService<INavigationAware>(context.ViewName);
8077

8178
view.DataContext = navigationAware;
82-
navigationAware.OnNavigatedTo(context);
79+
8380
if (navigationAware is ICanUnload canUnloadNavigationAware)
8481
{
8582
canUnloadNavigationAware.RequestUnload += () =>
8683
{
8784
DeActivate(context);
8885
};
8986
}
90-
Current.SetData((view, navigationAware));
87+
9188
context.View = view;
9289
ViewCache.AddOrUpdate(context, view, (key, value) => view);
9390
}
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;
94105
return view;
95106
}
96107

@@ -127,31 +138,7 @@ private IDataTemplate CreateRegionDataTemplate()
127138
{
128139
return null;
129140
}
130-
var view = context.View;
131-
if (view is null)
132-
{
133-
view = context.ServiceProvider.GetRequiredKeyedService<IView>(context.ViewName);
134-
var navigationAware = context.ServiceProvider.GetRequiredKeyedService<INavigationAware>(context.ViewName);
135-
136-
if (Current.TryTakeData(out var previousData))
137-
{
138-
previousData.NavigationAware.OnNavigatedFrom(context);
139-
}
140-
141-
view.DataContext = navigationAware;
142-
navigationAware.OnNavigatedTo(context);
143-
context.Alias = navigationAware.Alias;
144-
if (navigationAware is ICanUnload canUnloadNavigationAware)
145-
{
146-
canUnloadNavigationAware.RequestUnload += () =>
147-
{
148-
DeActivate(context);
149-
};
150-
}
151-
Current.SetData((view, navigationAware));
152-
context.View = view;
153-
ViewCache.AddOrUpdate(context, view, (key, value) => view);
154-
}
141+
var view = ResolveView(context);
155142
return view as Control;
156143
});
157144
}

0 commit comments

Comments
 (0)