Skip to content

Commit

Permalink
Fix: failing to correctly trigger the methods of the INavigationAware…
Browse files Browse the repository at this point in the history
… interface. (#1177)

* fix: failing to correctly trigger the methods of the INavigationAware interface.

when both the View and ViewModel inherit from the INavigationAware interface, only the OnNavigatedToAsync/OnNavigatedFromAsync methods of the View are triggered

* refactor: reduce code duplication.
  • Loading branch information
textGamex committed Aug 11, 2024
1 parent 2bd5fea commit d27947e
Showing 1 changed file with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,33 +184,42 @@ private void ApplyTransitionEffectToNavigatedPage(object content)

private static void NotifyContentAboutNavigatingTo(object content)
{
switch (content)
{
case INavigationAware navigationAwareNavigationContent:
_ = Task.Run(navigationAwareNavigationContent.OnNavigatedToAsync).ConfigureAwait(false);
break;
case INavigableView<object> { ViewModel: INavigationAware navigationAwareNavigableViewViewModel }:
_ = Task.Run(navigationAwareNavigableViewViewModel.OnNavigatedToAsync).ConfigureAwait(false);
break;
case FrameworkElement { DataContext: INavigationAware navigationAwareCurrentContent }:
_ = Task.Run(navigationAwareCurrentContent.OnNavigatedToAsync).ConfigureAwait(false);
break;
}
NotifyContentAboutNavigating(content, navigationAware => navigationAware.OnNavigatedToAsync());
}

private static void NotifyContentAboutNavigatingFrom(object content)
{
NotifyContentAboutNavigating(content, navigationAware => navigationAware.OnNavigatedFromAsync());
}

[System.Diagnostics.CodeAnalysis.SuppressMessage(
"ReSharper",
"SuspiciousTypeConversion.Global",
Justification = "The library user might make a class inherit from both FrameworkElement and INavigationAware at the same time."
)]
private static void NotifyContentAboutNavigating(object content, Func<INavigationAware, Task> function)
{
switch (content)
{
// The order in which the OnNavigatedToAsync/OnNavigatedFromAsync methods of View and ViewModel are called
// is not guaranteed
case INavigationAware navigationAwareNavigationContent:
_ = Task.Run(navigationAwareNavigationContent.OnNavigatedFromAsync).ConfigureAwait(false);
_ = Task.Run(() => function(navigationAwareNavigationContent)).ConfigureAwait(false);
if (
navigationAwareNavigationContent
is FrameworkElement { DataContext: INavigationAware viewModel }
&& !ReferenceEquals(viewModel, navigationAwareNavigationContent)
)
{
_ = Task.Run(() => function(viewModel)).ConfigureAwait(false);
}

break;
case INavigableView<object> { ViewModel: INavigationAware navigationAwareNavigableViewViewModel }:
_ = Task.Run(navigationAwareNavigableViewViewModel.OnNavigatedFromAsync)
.ConfigureAwait(false);
_ = Task.Run(() => function(navigationAwareNavigableViewViewModel)).ConfigureAwait(false);
break;
case FrameworkElement { DataContext: INavigationAware navigationAwareCurrentContent }:
_ = Task.Run(navigationAwareCurrentContent.OnNavigatedFromAsync).ConfigureAwait(false);
_ = Task.Run(() => function(navigationAwareCurrentContent)).ConfigureAwait(false);
break;
}
}
Expand Down

0 comments on commit d27947e

Please sign in to comment.