Reputation: 85
I have a global page that hosts navigation buttons to diffeernt pages. it also hosts a frame called ContentFrame. It navigates to the diffeernt pages as the navigation buttons are clicked. But initially some calls are made to the DB and when data is returned, the ContentFrame navigates to the mainpage first. I have a back button and a home button. When the contentframe has MainPage in it and the back button is clicked, I want the app to go back to a previous page from which this whole global page is loaded. If the content frame just has some other pages loaded, then I want it to simply go back to the previous page that was loaded when back button is clicked. Here is my code for the contentframe in the global xaml
<Frame x:Name="ContentFrame" Grid.Row="1" Grid.Column="1" />
Here is my back button click code for global page
private void Back_Click(object sender, RoutedEventArgs e)
{
if ((ContentFrame.CurrentSourcePageType != null) && (ContentFrame.CurrentSourcePageType.Name.Equals("MainPage")) && (this.navigationHelper.CanGoBack()))
{
ResetPageCache();
this.navigationHelper.GoBack();
}
else
{
if (this.ContentFrame.CanGoBack)
{
this.ContentFrame.GoBack();
}
else if (this.navigationHelper.CanGoBack())
{
this.navigationHelper.GoBack();
}
}
}
private void ResetPageCache()
{
var cacheSize = (this.Frame).CacheSize;
(this.Frame).CacheSize = 0;
}
The issue that I am facing is that when the ContentFrame has main page loaded and back button is clicked, it navigates back to a previous page before the global page was loaded but in the process, the onnavigatedfrom() event for MainPage is not raised where I unsubscribe some events. Any idea how I can get the onNavigatedFrom() to fire for MainPage in this case?
Thanks
Upvotes: 0
Views: 2602
Reputation: 31831
Sure. Do this:
var frame = Window.Current.Content as Frame;
frame.SetNavigationState(string.Empty);
Best of luck!
Upvotes: 0
Reputation: 31724
The simplest solution might be to handle the Unloaded
event apart from/instead of OnNavigatedFrom
so that your clean up happens regardless of navigation but rather when just being removed from the visual tree. You might want to pair it with the Loaded
event though in case your page gets added to the visual tree without being navigated to (e.g. when the cached parent page is being navigated to).
Upvotes: 2