wp7 odata v2 dataservicestate 保存和恢复方法需要墓碑示例
我正在寻找一个示例,说明如何在 WP7 应用程序中使用新的 DataServiceState Save 和 Restore 方法来墓碑数据上下文 - 我找不到任何示例,并且我使用的方法导致了异常,
会正确保存数据上下文
PhoneApplicationService.Current.State["DataContext"] = DataServiceState.Save(this.Model.Entities);
这 在应用程序重新激活后恢复它,
var dc = (PhoneApplicationService.Current.State["DataContext"] as DataServiceState).Restore();
但引发异常
An item could not be added to the collection. When items in a DataServiceCollection are tracked by the DataServiceContext, new items cannot be added before items have been loaded into the collection.
如果我尝试重新加载“直接”(不使用 DataServiceState.Save 方法)存储在 PhoneApplicationService.Current.State 中的数据上下文,这与我得到的异常相同。我找不到有关新 ODATA v2 DataServiceState 类或示例的任何官方文档。
谢谢 迈克尔
I am looking for an example of how to use the new DataServiceState Save and Restore methods in a WP7 application in order to tombstone a datacontext - I cannot find any examples and the approach I used resulted in an exception
this saves the data context correctly
PhoneApplicationService.Current.State["DataContext"] = DataServiceState.Save(this.Model.Entities);
this attempts to restore it after the app is re-activated
var dc = (PhoneApplicationService.Current.State["DataContext"] as DataServiceState).Restore();
but throws an exception
An item could not be added to the collection. When items in a DataServiceCollection are tracked by the DataServiceContext, new items cannot be added before items have been loaded into the collection.
This is the same exception I get if I try to reload a datacontext that I stored "directly" (without using the DataServiceState.Save method) in the PhoneApplicationService.Current.State. I cannot find any offical documentation on the new ODATA v2 DataServiceState class or examples.
thanks
Michael
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您确定吗? 第一个链接 - 页面底部。
Are you sure? First link - bottom of the page.
您尝试过这里的演练吗?
如何:保留和还原 Windows Phone 的应用程序状态
Have you tried the walkthrough here?
How to: Preserve and Restore Application State for Windows Phone
由于序列化和对象引用问题,存储和检索数据上下文很棘手。 MSFT 团队正在致力于改进 DataServiceState。我通过将 Save() 返回的 DataServiceState 存储在应用程序状态中,成功地保存和恢复了 WP7 应用程序中的上下文(就像您所做的那样)。然后在激活时,我首先实例化我的 DataServiceClient(其中包含上下文和 DataServiceCollection),然后在客户端中调用 RestoreData 方法并将检索到的 DataServiceState 传递给它。该方法恢复 DataServiceClient 内的上下文和 DSC。
Storing and retrieving datacontext is tricky due to serialization and object reference issues. The MSFT team is working on improving DataServiceState. I've succeeded in saving and restoring the context in a WP7 app, by storing the DataServiceState returned by Save() in the app state (just like you've done). Then on activated, I first instantiate my DataServiceClient (which contains the context and the DataServiceCollection), and then I call a RestoreData method in the client and pass the retrieved DataServiceState to it. The method restores the context and DSC within the DataServiceClient.