Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 20 additions & 33 deletions src/Lemon.ModuleNavigation.Avaloniaui/Regions/Region.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,40 @@ 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<IView>(context.ViewName);
var navigationAware = context.ServiceProvider.GetRequiredKeyedService<INavigationAware>(context.ViewName);

if (Current.TryTakeData(out var previousData))
{
previousData.NavigationAware.OnNavigatedFrom(context);
}
navigationAware = context.ServiceProvider.GetRequiredKeyedService<INavigationAware>(context.ViewName);

view.DataContext = navigationAware;
navigationAware.OnNavigatedTo(context);

if (navigationAware is ICanUnload canUnloadNavigationAware)
{
canUnloadNavigationAware.RequestUnload += () =>
{
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<INavigationAware>(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;
}

Expand Down Expand Up @@ -127,31 +138,7 @@ private IDataTemplate CreateRegionDataTemplate()
{
return null;
}
var view = context.View;
if (view is null)
{
view = context.ServiceProvider.GetRequiredKeyedService<IView>(context.ViewName);
var navigationAware = context.ServiceProvider.GetRequiredKeyedService<INavigationAware>(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;
});
}
Expand Down