如何导航到Windows手机中的全景页面?

时间:2013-10-29 14:08:42

标签: windows-phone-7 navigation

在我的主页面中,我尝试导航到另一个页面(这是一个全景页面),主页的我的c#代码是,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace Panoramatry
{
  public partial class MainPage : PhoneApplicationPage
  {
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        displsy();
    }
    public void display()
    {
        NavigationService.Navigate(new Uri("/GettingStarted.xaml",UriKind.Relative));
    }
  }
}

我的 GettingStarted.xaml 页面包含以下代码,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace Panoramatry
{
  public partial class GettingStarted : PhoneApplicationPage
  {
    public GettingStarted()
    {
        InitializeComponent();
        display();
    }
    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        display();
    }
    public void display()
    {
        MessageBox.Show("Navigation Success");
    }
  }
}

但是当在主页中执行导航代码时,我收到以下错误,

 An exception of type 'System.NullReferenceException' occurred in Panoramatry.DLL but was not handled in user code

但是当我在主页面上使用按钮,并将此导航添加到其点击事件时,它的工作完全正常! 可能是什么问题呢? 提前谢谢!

1 个答案:

答案 0 :(得分:2)

啊,好的,现在它更清楚了

您不能在页面构造函数中使用NavigationService,它不会被初始化。在导航的第一页中,将重定向移动到OnNavigatedTo事件,例如

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Microsoft.Phone.Controls;

 namespace Panoramatry
 {
   public partial class MainPage : PhoneApplicationPage
   {
    // Constructor
    public MainPage()
    {
      InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        display();
    }

   public void display()
   {
    NavigationService.Navigate(new Uri("/GettingStarted.xaml",UriKind.Relative));
   }
  }
}

顺便说一下,处理条件输入页面的更好方法是使用wp的UriMapping功能。查看示例here这样,您的后退按钮条目堆栈中没有不必要的页面,为您的用户提供更好的用户体验