WP7 上的隔离存储出现意外行为
我使用隔离存储时遇到一些意外行为。在本例中,我使用它来维护状态以达到逻辑删除的目的。
这是一个业余无线电执照考试练习应用程序。这是发生的事情。当应用程序首次运行时,用户位于 MainPage 上,并选择两个链接之一:OptionPage1 或 OptionPage2(它们的工作方式类似,因此我只关注其中一个)。在 OptionsPage1 上,用户从多个选项中进行选择,然后单击启动按钮转到 ExamPage,用户在其中回答问题。考试完成后,用户可以前往 ScorePage 查看分数。完成此操作后,选项之一是单击按钮返回主页。
每次用户选择某个操作时,都会使用隔离存储处理类库中的静态方法更新隔离存储中的文件。我观察到一种非常奇怪的行为。当 ScorePage 在用户单击返回 MainPage 时保存其状态时,会将位置存储为“MainPage”,因为这是用户在下一毫秒内将到达的位置。然而,当MainPage读取ScorePage刚刚保存的隔离存储文件时,它与ScorePage保存的内容并不相同!它具有 OptionPage1 保存的内容!我知道 ScorePage 正确保存了信息,因为我尝试在保存信息后立即阅读它,结果没问题。
看起来可能正在创建和更新文件的不同版本。但这没有意义。每个Page都使用完全相同的静态方法来保存和更新独立的存储文件,并且只有一个文件夹和文件名。我很困惑。
I am getting some unexpected behavior using Isolated Storage. In this case, I am using it to maintain state for the purpose of tombstoning.
This is a Ham Radio licensing exam practice app. Here is what happens. When the app is first run, the user is on the MainPage, and selects one of two links, OptionPage1 or OptionPage2 (they work similarly so I'll just focus on one). On OptionsPage1, the user selects from a number of options, then clicks a Launch button to go to ExamPage, where the user answers the questions. Upon completion of the exam, the user goes to ScorePage to see the score. When finished with this, one of the options is to click a button to go back to the MainPage.
Each time the user chooses some action, a file in isolated storage is updated using static methods in an isolated storage handling class library. I have observed a really odd behavior. When ScorePage saves its state when the user clicks to go back to MainPage, it stores location as "MainPage" because that's where the user will be in the next millisecond. However, when MainPage reads the isolated storage file that was just saved by ScorePage, it does not have the same contents that ScorePage saved! It has the contents that were saved by OptionPage1 !! I know ScorePage is saving the info correctly because I tried reading it immediately after it saved it and it was OK.
It almost looks like there might be different versions of the file being created and updated. But this makes no sense. Each Page is using the exact same static methods to save and update the isolated storage file, and there's only one folder and filename. I am very perplexed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Cyberherbalist,我不确定您是否使用此功能(我假设您是这样,或者您的应用程序会遇到一些严重的导航问题)在工作时存在陷阱与 非线性导航服务。基本上,如果您使用事件处理程序 OnNavieratedTo 保存到文件,或者在某些情况下 OnNavigedFrom,您将在之前保存的内容之上写入内容。换句话说,所有服务所做的就是调用 GoBack() 方法,在这种情况下,您将触发该方法以再次保存到文件中。
其工作原理如下:
如果是这种情况,请在 App.xaml.cs 文件中创建一个全局变量来存储是否参加了考试并完成。然后转到保存内容的任何页面,并使用“if”语句进行检查,以确定是转到流程中的下一页还是导航回主页。还要确保在保存到 ScorePage 上的文件后,您设置了考试已完成的全局变量。
Cyberherbalist, I'm not sure if your using this (I'm assuming you are or your app would run into some serious issues with navigation) there is a gotcha when working with the Non-Linear Navigation Service. Basically if your saving to the file using the event handlers OnNavigatedTo or in some cases OnNavigatedFrom your writing on top of your previously saved content. In other words all the service is doing is calling the GoBack() method in which case your firing the method to save to the file again.
Heres how it works:
If this is the case, create a global variable in the App.xaml.cs file that stores whether or not an exam was taken and finished. Then go to any pages your saving content and check with an "if" statement to determine whether your going to the next page in the process or your navigating back to the home page. Also make sure after you save to file on the ScorePage that you set the global variable that an exam was finished.