From 8522280dba6f5555a8452458a99bb8c10e4f4f1b Mon Sep 17 00:00:00 2001 From: Easley Date: Mon, 28 Jul 2025 10:15:52 +0800 Subject: [PATCH] https://github.com/NeverMorewd/Lemon.ModuleNavigation/issues/58 --- .../Regions/Region.cs | 53 +++++++------------ 1 file changed, 20 insertions(+), 33 deletions(-) diff --git a/src/Lemon.ModuleNavigation.Avaloniaui/Regions/Region.cs b/src/Lemon.ModuleNavigation.Avaloniaui/Regions/Region.cs index ffb8f7d..e1b0a10 100644 --- a/src/Lemon.ModuleNavigation.Avaloniaui/Regions/Region.cs +++ b/src/Lemon.ModuleNavigation.Avaloniaui/Regions/Region.cs @@ -68,18 +68,15 @@ public virtual void ScrollIntoView(NavigationContext item) protected IView? ResolveView(NavigationContext context) { var view = context.View; + INavigationAware? navigationAware = null; + if (view is null) { view = context.ServiceProvider.GetRequiredKeyedService(context.ViewName); - var navigationAware = context.ServiceProvider.GetRequiredKeyedService(context.ViewName); - - if (Current.TryTakeData(out var previousData)) - { - previousData.NavigationAware.OnNavigatedFrom(context); - } + navigationAware = context.ServiceProvider.GetRequiredKeyedService(context.ViewName); view.DataContext = navigationAware; - navigationAware.OnNavigatedTo(context); + if (navigationAware is ICanUnload canUnloadNavigationAware) { canUnloadNavigationAware.RequestUnload += () => @@ -87,10 +84,24 @@ public virtual void ScrollIntoView(NavigationContext item) DeActivate(context); }; } - Current.SetData((view, navigationAware)); + context.View = view; ViewCache.AddOrUpdate(context, view, (key, value) => view); } + else + { + navigationAware = view.DataContext as INavigationAware + ?? context.ServiceProvider.GetRequiredKeyedService(context.ViewName); + } + if (Current.TryTakeData(out var previousData)) + { + previousData.NavigationAware.OnNavigatedFrom(context); + } + + navigationAware.OnNavigatedTo(context); + + Current.SetData((view, navigationAware)); + context.Alias = navigationAware.Alias; return view; } @@ -127,31 +138,7 @@ private IDataTemplate CreateRegionDataTemplate() { return null; } - var view = context.View; - if (view is null) - { - view = context.ServiceProvider.GetRequiredKeyedService(context.ViewName); - var navigationAware = context.ServiceProvider.GetRequiredKeyedService(context.ViewName); - - if (Current.TryTakeData(out var previousData)) - { - previousData.NavigationAware.OnNavigatedFrom(context); - } - - view.DataContext = navigationAware; - navigationAware.OnNavigatedTo(context); - context.Alias = navigationAware.Alias; - if (navigationAware is ICanUnload canUnloadNavigationAware) - { - canUnloadNavigationAware.RequestUnload += () => - { - DeActivate(context); - }; - } - Current.SetData((view, navigationAware)); - context.View = view; - ViewCache.AddOrUpdate(context, view, (key, value) => view); - } + var view = ResolveView(context); return view as Control; }); }