An error is occur when we use navigation to move other page

≡放荡痞女 提交于 2020-01-04 05:08:35

问题


using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace WindowsPhoneApplication7
{
    public partial class Listbox : UserControl
    {
        public Listbox()
        {
            InitializeComponent();
        }

        private void listbox(object sender, MouseEventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/home.xaml", UriKind.Relative));
        }
    }
}

An error is occur ....... does not contain a definition for 'NavigationService' and no extension method 'NavigationService' accepting a first argument of type ' could be found (are you missing a using directive or an assembly reference?)


回答1:


NavigationService is a property on the PhoneApplicationPage class. You are not deriving from that class, you are deriving from UserControl.

You need to get the parent phone page the user control is on and get the NavigationService reference from there.

Your compiler error is because it cannot locate a definition for NavigationService on the Listbox class you have made.




回答2:


What Adam said is correct. But a easy solution is to define following static utility methods in App.xaml.cs

public static PhoneApplicationFrame CurrentRootVisual
{
    get
    {
        return (App.Current.RootVisual as PhoneApplicationFrame);
    }
}

public static bool Navigate(Uri source)
{
    if (CurrentRootVisual != null)
        return CurrentRootVisual.Navigate(source);

    return false;
}

public static void GoBack()
{
    if (CurrentRootVisual != null)
        CurrentRootVisual.GoBack();
}

Then you can just do:

App.Navigate(yourNavigateUri)

or App.GoBack()

From anywhere you like!




回答3:


Dispatcher.BeginInvoke(() =>
    NavigationService.Navigate(new Uri("/home.xaml", UriKind.Relative)));


来源:https://stackoverflow.com/questions/6830123/an-error-is-occur-when-we-use-navigation-to-move-other-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!