C# Silverlight 3 - 如何使用编程方式在页面之间导航?

8
我有一个使用C# Silverlight 3开发的应用程序,其中包含多个页面。第一页名为“首页”,第二页名为“详情”。唯一的导航方式是通过编程实现。我该如何实现这一点?!我已经在各处寻找答案,但只找到了XAML URI映射器的实现方法...非常感谢您的帮助。

Silverlight 的哪个版本? - BigBlondeViking
Silverlight 3........... - Goober
5个回答

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>

即使您之前说过不需要,您的“详细信息”页面也应该进行映射。

7
C#代码:App.Current.Host.NavigationState = "/Welcome";
XAML语言。

7

您尝试过NavigationService吗?

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

(此代码可用于导航到名为“Details.xaml”的页面)

在Silverlight中,URI引用是相对于XAP文件的。如果detailspage位于项目的根目录下,其Uri应为'new Uri("/Details.xaml", UriKind.Relative)。 - Kees Kleimeer
我向你保证,这绝对行不通。全新的Silverlight商业应用程序模板,使用那种方法导航到任何页面都不起作用。 - Goober
你是在MVVM应用程序中尝试从ViewModel导航,还是从xaml.cs导航? - Neil

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());

我已经到处寻找这个答案了,如果我能得到它,那就加3分吧。 - MJ33

2

试着使用这个。这对我很有效。

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

尝试使用此代码。这在我的情况下有效。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接