【问题标题】:Windows phone 8 app, An exception of type 'System.NullReferenceException' occurred during NavigationServiceWindows phone 8 应用程序,在 NavigationService 期间发生“System.NullReferenceException”类型的异常
【发布时间】:2014-08-22 14:14:47
【问题描述】:

我有一个问题,Windows phone 8 应用程序在 this.NavigationService.Navigate(new Uri("/Dashboard.xaml", UriKind.Relative));行,因为它试图导航到新页面。应用程序应该加载到欢迎页面上,即下面的页面,检查它是否是用户第一次打开应用程序,如果是,它应该留在该页面上,直到用户单击按钮继续。但如果用户不是第一次打开应用程序,它应该检查然后直接进入仪表板。但是错误就在这里,它不想导航,因为它显示了下面的错误。我查看了有关此错误消息的所有其他帖子,但没有任何答案可以帮助解决当前的情况。

这是给出的错误信息;

Good Morning Dashboard.DLL 中出现“System.NullReferenceException”类型的异常,但未在用户代码中处理。附加信息:对象引用未设置为对象的实例。如果有这个异常的处理程序,程序可以安全地继续。

这是代码

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Good_Morning_Dashboard.Resources;
using System.IO.IsolatedStorage;

namespace Good_Morning_Dashboard
{
    public partial class MainPage : PhoneApplicationPage
    {
        public bool trueOrFalse;
        public string result;

        public MainPage()
        {
            InitializeComponent();

            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
            if (!settings.Contains("DataKey"))
            {
                settings.Add("DataKey", "First Time");

            }
            else
            {
                settings["DataKey"] = "Not First Time";
                this.NavigationService.Navigate(new Uri("/Dashboard.xaml", UriKind.Relative));
            }

            settings.Save();



        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Dashboard.xaml", UriKind.Relative));
        }
    }
}

提前感谢您! :)

【问题讨论】:

    标签: c# windows-phone-8 isolatedstorage


    【解决方案1】:

    NavigationService 的使用位置存在限制。我相信您不能在页面构造函数中使用它,因为那时它还没有准备好。您可能希望将其放在 Page.OnNavigatedTo 覆盖中:

        public MainPage()
        {
            InitializeComponent();
        }
    
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
            if (!settings.Contains("DataKey"))
            {
                settings.Add("DataKey", "First Time");
    
            }
            else
            {
                settings["DataKey"] = "Not First Time";
                this.NavigationService.Navigate(new Uri("/Dashboard.xaml", UriKind.Relative));
            }
    
            settings.Save();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多