8

Say I have a C# Silverlight 3 application with a number of pages. The first page is called Home, and the second page is called Details. The only way to navigate to details is programmatically. How do I do this?! Looked everywhere for the answer and all i've found are xaml uri mapper implementations....

Help greatly appreciated

2
  • Which version of Silverlight? Sep 30, 2009 at 13:21
  • Silverlight 3...........
    – Goober
    Sep 30, 2009 at 13:25

5 Answers 5

7

Have you tried the NavigationService?

this.NavigationService.Navigate(new Uri("Details.xaml", UriKind.Relative));

3
  • In Silverlight URI reverenses are relative to the XAP. The Uri for the detailspage (if it's in the root of your project) should be 'new Uri("/Details.xaml", UriKind.Relative) Sep 30, 2009 at 14:21
  • I'm telling you 100% this does not work. Brand new Silverlight Business Application Template, Trying to navigate to literally any page doesn't work using that method.
    – Goober
    Sep 30, 2009 at 14:32
  • Are you trying to navigation from ur ViewModel in a MVVM app, or from the xaml.cs?
    – Neil
    Oct 1, 2009 at 15:25
7

c#:

this.navContent.Navigate(new Uri("Welcome", UriKind.Relative));

XAML:

<navigation:Frame
    x:Name="navContent"
    HorizontalContentAlignment="Stretch"
    VerticalContentAlignment="Stretch"
    Source="Welcome">
    <navigation:Frame.UriMapper>
        <uriMapper:UriMapper>
            <uriMapper:UriMapping Uri="Welcome" MappedUri="/Views/Welcome.xaml" />
            <uriMapper:UriMapping Uri="Profile" MappedUri="/Views/Profile.xaml" />
            <uriMapper:UriMapping Uri="Details/{id}" MappedUri="/Views/Details.xaml?photoid={id}" />
        </uriMapper:UriMapper>
    </navigation:Frame.UriMapper>
</navigation:Frame>

Even your "details" page should be mapped (despite what you said.)

7

C# App.Current.Host.NavigationState = "/Welcome";

XAML

0
5

The best solution is:

Add this code to your App.xaml.cs:

private static Grid root;

private void Application_Startup(object sender, StartupEventArgs e)
{
    root = new Grid();
    root.Children.Add(new MainPage());

    this.RootVisual = root;
}

public static void Navigate(UserControl newPage)
{
    UserControl oldPage = root.Children[0] as UserControl;

    root.Children.Add(newPage);
    root.Children.Remove(oldPage);
}

And then, to navigate between pages, you'll just have to call:

App.Navigate(new OtherSamplePage());
1
  • I've been searching everywhere for this answer +3 if I could
    – MJ33
    Oct 27, 2014 at 18:17
2

Try using this. This worked for me.

((System.Windows.Controls.Frame)(this.Parent)).Navigate(new Uri("/Import",UriKind.Relative));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.