Silverlight - How to navigate from a User Control to a normal page?

Viewed 24395

If I do this inside a User Control:

NavigationService.Navigate(new Uri("/Alliance.xaml", UriKind.Relative));

it says this error:

An object reference is required for the non-static field, method, or property 'System.Windows.Navigation.NavigationService.Navigate(System.Uri)'

Thank you


Well, I solved passing the normal Page as an argument to the User Control, so I could get the NavigationService.

8 Answers
(Application.Current.RootVisual as PhoneApplicationFrame).Navigate(uri);

I normally use an EventHandler. Example: in your user control, define something like

public event EventHandler goToThatPage;

that you will call in your control foe example like this:

goToThatPage(this, new EventArgs());

Then in the constructor of your MainPage.xaml.cs (if the user control is contained there) you will define:

uxControlName.goToThatPage+= new EventHandler(ControlGoToThatPage);

and somewhere in your MainPage.xaml.cs you finaly declare the action to be done:

    void ControlGoToThatPage(object sender, EventArgs e)
    {
        this.NavigationService.Navigate(new Uri("/Pages/ThatPage.xaml", UriKind.Relative));
    }
Related