6

When a Page is navigated to in silverlight you can override this method.

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
}

The NavigationEventArgs has a NavigationMode enumeration which is defined as

public enum NavigationMode
{
    New = 0,
    Back = 1,
    Forward = 2,
    Refresh = 3,
}

But calling e.NavigationMode always throws a NotImplementedException

Is there a way in silverlight to detect a page is being navigated to because the user hit the forward/back browser button.

What I am trying to achieve is some kind of state that can be preserved when the user hits the back button.

For example assume you have a customer page which is showing a list of customers in a datagrid. The user can select a customer and there is a detail view which shows all the orders for that customer. Now within an order item you can click a hyperlink link that takes you to the shipping history of the order which is a separate page. When the user hits the back button I want to go back to the customers page and automatically select the customer he was viewing. Is this possible at all ?

I also tried out the fragment navigation feature

NavigationService.Navigate(new Uri("#currentcustomerid=" 
       + customer.Id.ToString(), UriKind.Relative));

when the customer selection changes but this adds too many items to the history when the user clicks various customers on the customer page.

EDIT

There is also an method you can override

protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
}

which is the same as handling the NavigationService.Navigating event as indicated by BugFinder's answer. In this method e.NavigationMode always returns New when when you hit the Back or Forward Button. The only time this method returns Back is when you explicitly call NavigationService.GoBack()

parapura rajkumar
  • 24,045
  • 1
  • 55
  • 85

4 Answers4

1

The

public enum NavigationMode 
{ 
    New = 0, 
    Back = 1, 
    Forward = 2, 
    Refresh = 3, 
} 

applies to the Navigating event..

if I do

_ns.Navigating += ns_Navigating;
        void ns_Navigating(object sender, NavigatingCancelEventArgs e)
        {

            if (SecurityCheck(e.Uri.OriginalString)) return;
            e.Cancel = true;
            ShowError("You are not authorised to view this page");
        }

I can see there that e.NavigationMode is set. You could do your test there?

BugFinder
  • 17,474
  • 4
  • 36
  • 51
1

I don't think there are any easy ways to do it out of the box, as far as I know.

What you are trying to achieve can be easily done using a framework I created at http://ultimateframework.codeplex.com

What I have done is to mesh the silverlight navigation frame and prism navigation together, so you will need unity and prism and mvvm friendly.

What you want to achieve can be done using the framework in the following ways

1) Implement IsNavigationTarget and returns true --> which will keep the same instance when navigating back, therefore, keeping the selection/selected item.

2) Access the onnavigatedto's journal to track where you came from, say /item/1 was the previous stack, so you know back button has been pressed from item 1.

3) You can even implement your own back/forward/refresh within the custom control provided for achieving the same result (not in codeplex yet)

I actually use it for production code at work, and I created it, so please feel free to try it out. Note that the version on there is buggy, and I have yet to have time to release our latest build, but should you require it, I will update it for you :), just pm me.

Joshscorp
  • 1,832
  • 4
  • 22
  • 42
1

Set a variable in the usercontrol containing the contentframe which indicates what customer is active.

Add a handler for the contentframe's Navigated event in the usercontrol. Use this to check the variable indicating what customer is active (if the variable is not null), and to select the customer.

Peter Dongan
  • 1,762
  • 12
  • 17
  • I think you missed the point of the question. The fact is when I page is navigated to I cannot figure out whether it is because of backward navigation or fresh navigation. How will storing the state help ? – parapura rajkumar Sep 10 '12 at 15:06
  • Because you can tell where they are coming from. If they navigate from the second page to the first via other means than the back button, then the customer would be displayed regardless alright - but perhaps that would not be undesirable anyway. – Peter Dongan Sep 10 '12 at 15:28
0

This might be what you are looking for:

http://jakkaj.wordpress.com/2008/09/08/control-silverlight-by-using-browser-back-and-foward-buttons/

Gambit
  • 2,455
  • 18
  • 20