8
votes

I'm still new with the Windows Phone development. So now I'm going to develop the Windwos Phone 8.1. I really no idea what's the problem with the page navigation. I wrote the code like this

private void hbGo_Click(object sender, RoutedEventArgs e)
{
    this.Frame.Navigate(typeof(SecondPage));
}

but it shows me the error (This page does not contain a definition for "Frame" and no extension method "Frame" accepting the first arguments) even i put like the code of bottom also the same...

Frame.Navigate(typeof(SecondPage));
3
Well, not sure it will help but the error usually means that "Frame" isn't part of your Page instance.Shalin Ved
@ShalinVed Yup...the error is means like this..so what should I do to prevent the error occur?jefferyleo
Can you try.. Frame rootFrame = Window.Current.Content as Frame rootFrame.Navigate(typeof(SecondPage));Shalin Ved

3 Answers

22
votes

The Navigation depends on the kind of your project:

If it is Windows Phone 8.1 Silverlight then you should use NavigationService.Navigate() method:

Applies to: Windows Phone 8 and Windows Phone Silverlight 8.1 | Windows Phone OS 7.1

If you are targeting Windows Phone RunTime then you should use Frame.Navigate method():

Minimum supported phone Windows Phone 8.1 [Windows Runtime apps only]

3
votes

Frame isn't a part of a Page. I do navigation the following way

NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.Relative));

you just have to pass the name of the xaml page you want to navigate to.

2
votes

I use this little Navigation Service class I created to allow me to navigate different pages from within the ViewModel of my Windows Phone 8.1 app. FYI, INavigate is part of Windows.UI.Xaml.Controls.

public class NavigationService : INavigate
{
    private Frame Frame { get { return (Frame)Window.Current.Content; } }

    public bool Navigate(Type sourcePageType)
    {
       return Frame.Navigate(sourcePageType);
    }
    public void Navigate(Type sourcePageType, object parameter)
    {
        Frame.Navigate(sourcePageType, parameter);
    }

    public void ClearStack()
    {
        ((Frame)Window.Current.Content).BackStack.Clear();
    }

    /// <summary>
    /// Virtual method used by the <see cref="GoBackCommand"/> property
    /// to invoke the <see cref="Windows.UI.Xaml.Controls.Frame.GoBack"/> method.
    /// </summary>
    public virtual void GoBack()
    {
        if (this.Frame != null && this.Frame.CanGoBack) this.Frame.GoBack();
    }

    /// <summary>
    /// Virtual method used by the <see cref="GoBackCommand"/> property
    /// to determine if the <see cref="Frame"/> can go back.
    /// </summary>
    /// <returns>
    /// true if the <see cref="Frame"/> has at least one entry 
    /// in the back navigation history.
    /// </returns>
    public virtual bool CanGoBack()
    {
        return this.Frame != null && this.Frame.CanGoBack;
    }
}