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

OnNavigatedFrom support #559

Merged
merged 1 commit into from
Mar 15, 2023
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
25 changes: 25 additions & 0 deletions src/Wpf.Ui/Controls/Navigation/NavigationViewContentPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ protected override void OnInitialized(EventArgs e)
return;

NotifyContentAboutNavigatingTo(eventArgs.Content);

if (eventArgs.Navigator is not NavigationViewContentPresenter navigator)
return;

NotifyContentAboutNavigatingFrom(navigator.Content);
};

Navigated += static (sender, eventArgs) =>
Expand All @@ -88,6 +93,14 @@ protected override void OnInitialized(EventArgs e)

self.ApplyTransitionEffectToNavigatedPage(eventArgs.Content);
};

Unloaded += (sender, _) =>
{
if (sender is NavigationViewContentPresenter navigator)
{
NotifyContentAboutNavigatingFrom(navigator.Content);
}
};
}

protected override void OnMouseDown(MouseButtonEventArgs e)
Expand All @@ -112,6 +125,18 @@ private static void NotifyContentAboutNavigatingTo(object content)
navigationAwareCurrentContent.OnNavigatedTo();
}

private static void NotifyContentAboutNavigatingFrom(object content)
{
if (content is INavigationAware navigationAwareNavigationContent)
navigationAwareNavigationContent.OnNavigatedFrom();

if (content is INavigableView<object> { ViewModel: INavigationAware navigationAwareNavigableViewViewModel })
navigationAwareNavigableViewViewModel.OnNavigatedFrom();

if (content is FrameworkElement { DataContext: INavigationAware navigationAwareCurrentContent })
navigationAwareCurrentContent.OnNavigatedFrom();
}

private void ApplyTransitionEffectToNavigatedPage(object content)
{
if (TransitionDuration < 1)
Expand Down