.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > NavigationService.Navigate vs. NavigationService.GoBack
 

NavigationService.Navigate vs. NavigationService.GoBack

I have a question about the NavigationService and Page Lifecycle. I read all the blogs I could find out there which include the following:

http://www.paulstovell.com/blog/inductive-uis-with-wpf-navigation-applications
http://www.lhotka.net/weblog/IssuesWithWPFPageNavigation.aspx
http://msdn.microsoft.com/en-us/library/system.windows.controls.page.keepalive.aspx
http://msdn.microsoft.com/en-us/library/ms750478.aspx

however I cannot figure why I am getting the following behavior. So basically this is what is happening. I have a Page that has the KeepAlive property set to true:

<PageKeepAlive="True"Loaded="Page_Loaded"Unloaded="Page_Unloaded">

Now when I navigate to the next page (let's call it Page2) and than call this.NavigationService.GoBack()from Page2it works perfect. The Page_Loaded event is called however the page's Constructor and OnInitialized methodIS NOT called as I would expect since the page has been told to keep alive.

The issue I have is if I navigate to this page via the NavigationFrame.Navigate() method (which is froma menu item in this case) the Constructor and OnInitialized method IS called. This seems contradicting to what I have seen in blogs and documentation. I am navigating using the Navigate(Uri) overload as follows:

NavigationFrame.Navigate(newUri(NavigationMenuManager.GetPageUrl(menuItem.Bookmark),UriKind.RelativeOrAbsolute));

NavigationMenuManager is just an internal class that gets the page Uri...

So my questions is this. When navigating to a page that has KeepAlive set to truethat has already been navigated to oncevia the NavigationFrame.Navigate() method should it be calling the Constructor and OnInitialized? If no, then is there any way to navigate directly to a page within the Journal History so that the page is not instantiated again? Basically what I want is for the page to stay alive the life of the application and if you ever navigate to it again it brings up the existing page.

Thanks!
Anthony
Anthony Coelho
So I solved the issue just not the way I originally planned on it. It appears that creating a new page (calling the Constructor and OnInitialized) when navigating to a page via a Uri that is already present in the Journal Stack is by design.

How I solved the issue was to create the Page by doing the following:

Page page = Application.LoadComponent(new Uri("[PageUri]", UriKind.RelativeOrAbsolute)) as Page;

["PageUri"] needs to be in the raw format which is:

\[Assembly Name];Component\[Resource Path]

ex.

\MyProject;Component\Pages\MyPage.xaml

Then I just use the Enterprise Caching Library to check to see if the page is already in cache and is it retrieves it from cache and if not it creates it and adds it to cache and navigates to the page via:

NavigationService.Navigate(page)

Thanks,
Anthony
Anthony Coelho
So I solved the issue just not the way I originally planned on it. It appears that creating a new page (calling the Constructor and OnInitialized) when navigating to a page via a Uri that is already present in the Journal Stack is by design.

How I solved the issue was to create the Page by doing the following:

Page page = Application.LoadComponent(new Uri("[PageUri]", UriKind.RelativeOrAbsolute)) as Page;

["PageUri"] needs to be in the raw format which is:

\[Assembly Name];Component\[Resource Path]

ex.

\MyProject;Component\Pages\MyPage.xaml

Then I just use the Enterprise Caching Library to check to see if the page is already in cache and is it retrieves it from cache and if not it creates it and adds it to cache and navigates to the page via:

NavigationService.Navigate(page)

Thanks,
Anthony
Anthony Coelho
Thanks for your post,its a great help

regards, Muralidhar(India)
Muralidhar SP

You can use google to search for other answers

Custom Search

More Threads

• Windows Forms Modal Dialog from WPF
• WindowsFormsHost z-order
• Hitdetection with WPF (on Ink in InkCanvas)
• ContextMenu from code - how do I create an overall click event?
• Adding to XAML Intellisense
• Rendering controls (Custom Panel) into image
• Memory Leak problem when using bitmap image as tooltip?
• How add just an item each time into listView using c#?
• Custom Panel + IScrollInfo + ListBox, How it works?
• nested listbox and templates