8

我还是 Windows Phone 开发的新手。所以现在我要开发Windwos Phone 8.1。我真的不知道页面导航有什么问题。我写了这样的代码

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

但它向我显示了错误(此页面不包含“框架”的定义,并且没有扩展方法“框架”接受第一个参数)即使我把底部的代码也一样......

Frame.Navigate(typeof(SecondPage));
4

3 回答 3

22

导航取决于您的项目类型:

如果是Windows Phone 8.1 Silverlight,那么您应该使用NavigationService.Navigate() 方法

适用于:Windows Phone 8 和 Windows Phone Silverlight 8.1 | 视窗电话操作系统 7.1

如果您的目标是Windows Phone RunTime,那么您应该使用Frame.Navigate method()

支持的最低手机 Windows Phone 8.1 [仅限 Windows 运行时应用]

于 2014-04-24T05:58:27.387 回答
3

框架不是页面的一部分。我通过以下方式进行导航

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

您只需传递要导航到的 xaml 页面的名称。

于 2014-04-24T05:29:17.877 回答
2

我使用我创建的这个小导航服务类来允许我从我的 Windows Phone 8.1 应用程序的 ViewModel 中导航不同的页面。仅供参考,INavigate 是 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;
    }
}
于 2014-08-18T19:33:44.297 回答