WP7 页面导航..Null 异常

WP7 Page Navigation..Null exception

提问人:Ahmed.C 提问时间:11/18/2013 更新时间:11/20/2013 访问量:94

问:

我正在制作该应用程序,它涉及使用文本文件等编写本地帐户。首次运行时,该应用程序旨在导航到名为“MainPage”的页面,但是,如果该文件“FTR.dat”不存在于此特定文件夹中,则它将导航到“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

谢谢!

Windows-Phone-7 导航 NullReferenceException

评论

0赞 Kevin Gosse 11/18/2013
让我猜猜:您是从页面的构造函数调用此代码的
0赞 Ahmed.C 11/20/2013
嗨,我很抱歉回复晚了!但是,是的,如果你是这个意思,我是从主页上调用它的。那么我该如何执行呢?

答:

1赞 Kevin Gosse 11/20/2013 #1

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

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