Sep 11 2006

Navigating XBAP to a different page

Category: IT Related | .NetRory Primrose @ 08:42

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.

Tags:

Comments (8) -

1.
Rob Relyea Rob Relyea says:

Rory-
You can get the NavigationService object in an easier way when you are programming within a page.

this.NavigationService.Navigate(page);

Lots of helpful people on the Forum...in case you haven't found it yet: forums.microsoft.com/.../ShowForum.aspx

-Rob
Program Manager, WPF Team

2.
Rory Primrose Rory Primrose says:

Thanks Rob. I did end up with this solution when the navigation was invoked from the page.

The issue I was having was that I was trying to invoke a navigation from a UserControl and NavigationService isn't a property of UserControl like it is on Page, but the MSDN Mag article was great.

3.
Nigel Stratton Nigel Stratton says:

I think you saved my hair, sanity or computer. Day two in WPF and I could not open a new "form" gosh how frustrating.

Thank you!!!

4.
Proxyvon Proxyvon says:

very simple and straight forward example...
cheers!!!

5.
Miguel Miguel Spain says:

Thank you a lot!! That's what I was looking for!!!!

6.
Homam Homam Syria says:

Thanks for the article. Actually I had a related problem with navigation in xBap that when I try to navigate to an asp.net page then two navigation actions is occurred.

I mean, I created an aspx page and I mad it write a log, then when a redirect is made to this page from an xBap application, then, I always find two redirects is occurred.

Have you seen this problem before?
Thanks in advance,

Homam.

7.
Rory Primrose Rory Primrose Australia says:

Hi Homam,

I have no idea. I haven't played with xbap applications since around the time I wrote this post  

A good place to ask this question is on the MSDN forums. The Microsoft people there are usually very responsive. You could also try stackoverflow.com.

8.
Homam Homam Syria says:

Thank you Roy for your response. I already added a question on Stackoverflow and I'll do on Microsoft forums.
Thanks again.

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading