Silverlight - 如何从用户控件导航到普通页面?

Ala*_*lan 15 .net c# silverlight windows-phone-7

如果我在用户控件中执行此操作:

NavigationService.Navigate(new Uri("/Alliance.xaml", UriKind.Relative));
Run Code Online (Sandbox Code Playgroud)

它说这个错误:

非静态字段,方法或属性'System.Windows.Navigation.NavigationService.Navigate(System.Uri)'需要对象引用

谢谢


好吧,我解决了将普通的页面作为参数传递给用户控件,所以我可以得到NavigationService.

Wal*_*eed 50

(Application.Current.RootVisual as PhoneApplicationFrame).Navigate(uri);
Run Code Online (Sandbox Code Playgroud)

  • 为什么这个答案没有标记为正确答案. (3认同)

ila*_*ila 12

我通常使用EventHandler.示例:在您的用户控件中,定义类似的内容

public event EventHandler goToThatPage;
Run Code Online (Sandbox Code Playgroud)

你将在你的控制对象中调用这样的例子:

goToThatPage(this, new EventArgs());
Run Code Online (Sandbox Code Playgroud)

然后在MainPage.xaml.cs的构造函数中(如果用户控件包含在那里),您将定义:

uxControlName.goToThatPage+= new EventHandler(ControlGoToThatPage);
Run Code Online (Sandbox Code Playgroud)

在您的MainPage.xaml.cs中的某个地方,您最终会声明要执行的操作:

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


Gon*_*ing 1

NavigationService 是一个类。Navigate 是您可以在该类的实例上调用的方法。它不是可以从对象引用外部调用的静态方法。

基本上,您需要获取当前页面的当前 NavigationService。此链接http://msdn.microsoft.com/en-us/library/system.windows.navigation.navigationservice.aspx应该有所帮助。