C#Silverlight 3 - 以编程方式在页面之间导航?

时间:2009-09-30 13:19:49

标签: c# silverlight silverlight-3.0 navigation

假设我有一个包含多个页面的C#Silverlight 3应用程序。第一页称为Home,第二页称为Details。导航到详细信息的唯一方法是以编程方式。我该怎么做呢?!到处寻找答案,我发现的都是xaml uri mapper implementation ....

非常感谢

5 个答案:

答案 0 :(得分:7)

您是否尝试过NavigationService?

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

答案 1 :(得分: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>

即使您的“详细信息”页面也应该映射(尽管您说的是。)

答案 2 :(得分:7)

C# App.Current.Host.NavigationState =“/ Welcome”;

XAML

答案 3 :(得分:5)

最佳解决方案是:

将此代码添加到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);
}

然后,要在页面之间导航,您只需要致电:

App.Navigate(new OtherSamplePage());

答案 4 :(得分:2)

尝试使用此功能。这对我有用。

((System.Windows.Controls.Frame)(this.Parent))。导航(新的Uri(“/ Import”,UriKind.Relative));