【问题标题】:how to navigate to a panorama page in windows phone?如何导航到 windows phone 中的全景页面?
【发布时间】:2013-10-30 15:52:04
【问题描述】:

在我的主页中,我尝试导航到另一个页面(这是一个全景页面),我的主页 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

但是当我在主页上使用一个按钮,并将这个导航添加到它的点击事件中时,它工作得非常好! 可能是什么问题呢? 提前致谢!

【问题讨论】:

  • 我从未听说过 Panoramatry.DLL,您的解决方案中有哪些第三方库?最重要的是,GettingStarted.xaml 中引用了哪些?
  • @FunksMaName 没有第三方库!!
  • 尝试两件事,当你取下 display();打电话,你还有同样的问题吗?如果没有,将显示移动到 OnNavigatedTo 事件块中,查看是否仍然抛出错误 protected override void OnNavigatedTo(NavigationEventArgs e) { MessageBox.Show("Navigation Success"); base.OnNavigatedTo(e); }
  • 仅供参考 Panoramatry 是我的项目名称!
  • 更新了我的问题!请看一下!

标签: windows-phone-7 navigation


【解决方案1】:

啊,好的,现在更清楚了

您不能在页面构造函数中使用 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 这样,您的后退按钮条目堆栈中就没有不必要的页面,为您的用户提供更好的用户体验

【讨论】:

  • 当我使用您的代码时,我收到一条错误消息,指出错误 1 ​​'Panoramatry.MainPage.OnNavigatedTo(Microsoft.Phone.Controls.NavigatingEventArgs)':找不到合适的方法来覆盖
  • 我已经更新了答案,按照答案中的方式复制并粘贴它,它肯定可以工作,您报告的最后一个错误仅在 OnNavigatedTo 拼写不正确或签名不正确时发生错了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多