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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1498071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:09:06  来源:igfitidea点击:

C# Silverlight 3 - Programmatically Navigate Between Pages?

c#silverlightsilverlight-3.0navigation

提问by Goober

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....

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

Help greatly appreciated

非常感谢帮助

采纳答案by Kees Kleimeer

Have you tried the NavigationService?

你试过导航服务吗?

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

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

回答by vidalsasoon

c#:

C#:

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

XAML:

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.)

甚至你的“详细信息”页面也应该被映射(不管你怎么说。)

回答by Vijay

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

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

XAML

XAML

回答by Eklavya Gupta

Try using this. This worked for me.

尝试使用这个。这对我有用。

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

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

回答by Bruno Corte

The best solution is:

最好的解决办法是:

Add this code to your App.xaml.cs:

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