【问题标题】:WP7 Page Navigation..Null exceptionWP7 页面导航..空异常
【发布时间】:2013-11-30 19:48:03
【问题描述】:

我正在制作应用程序,它涉及使用文本文件等创建本地帐户。第一次运行时,应用程序旨在导航到名为“MainPage”的页面,但如果文件“FTR.dat”没有t 存在于此特定文件夹中,然后它将导航到“CreateAccount”页面,但如果文件“FTR.dat”确实存在于该特定文件夹中,那么它将导航到“MainPage”

但我得到了这个 nullrefrenceexception 错误: 这是我的代码:

  Dim myIsolatedStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
    If myIsolatedStorage.FileExists("PasswordManagerAccount/FTR.dat") = False Then

        NavigationService.Navigate(New Uri("/CreateAccount.xaml", UriKind.Relative))

    ElseIf myIsolatedStorage.FileExists("PasswordManagerAccount/FTR.dat") = True Then

        NavigationService.Navigate(New Uri("/MainPage.xaml", UriKind.Relative))

    End If

谢谢!

【问题讨论】:

  • 让我猜猜:你是从页面的构造函数调用这段代码
  • 嗨,很抱歉回复晚了!但是,是的,如果这就是您的意思,我是从 MainPage 调用它的。那么我该如何执行呢?

标签: windows-phone-7 navigation nullreferenceexception


【解决方案1】:

您不能从页面构造函数中调用此类代码,因为导航服务尚未初始化。将其移至Loadedevent 或OnNavigatedTo

Protected Overrides Sub OnNavigatedTo(ByVal e As System.Windows.Navigation.NavigationEventArgs)
    Dim myIsolatedStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
    If myIsolatedStorage.FileExists("PasswordManagerAccount/FTR.dat") = False Then
        NavigationService.Navigate(New Uri("/CreateAccount.xaml", UriKind.Relative))
    ElseIf myIsolatedStorage.FileExists("PasswordManagerAccount/FTR.dat") = True Then
        NavigationService.Navigate(New Uri("/MainPage.xaml", UriKind.Relative))
    End If
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 2011-09-17
    相关资源
    最近更新 更多