Navigating XBAP to a different page
Surprisingly, one of the first major problems I had with WPF was when I tried to figure out how to navigate to a new WPF page inside an XBAP application. Sure, there are a few controls which encapsulate this functionality (like the Frame and Hyperlink controls), but I wanted to fire a navigation from a button click or double click of a listbox item. I was researching the net for a day and a half and coming up empty.
Could it be this difficult? Should it be? The answer to these questions is no, but there just isn’t enough information out there because WPF is still and emerging technology that is not widely adopted (it’s still only just turned RC1 after all).
One of the bits of information I came across was from the MSDN Magazine. I found the following code sample a little suspect though:
void viewHyperlink_Click(object sender, RoutedEventArgs e)
{
// View Order
ViewOrderPage page = new ViewOrderPage(GetSelectedOrder());
NavigationWindow window =
(NavigationWindow)this.Parent; // Don’t do this!
window.Navigate(page);
}
It said "Don’t do this!", but I was desperate and tried it anyway. It didn’t work. Thankfully, later on in the article, it gave an example of how it is done in a way that works:
void viewHyperlink_Click(object sender, RoutedEventArgs e)
{
// View Order
ViewOrderPage page = new ViewOrderPage(GetSelectedOrder());
NavigationService ns =
NavigationService.GetNavigationService(this);
ns.Navigate(page);
}
That’s better!
The MSDN Magazine article I found (App Fundamentals, Build A Great User Experience With Windows Presentation Foundation) is still a very good article despite the code above.