Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: failing to correctly trigger the methods of the INavigationAware interface. #1177

Merged
merged 2 commits into from
Aug 11, 2024
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
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
Loading