简体   繁体   English

将条目从在线XML文件写入隔离存储

[英]Writing entries from online XML file to isolated storage

I have a simple Silverlight WP7 app that reads the contents of an online XML file and displays them. 我有一个简单的Silverlight WP7应用程序,该应用程序读取在线XML文件的内容并显示它们。 However, there are maybe 150 items total and scrolling through them all can be quite inconvenient. 但是,总共可能有150个项目,并且滚动浏览它们可能非常不便。 So, I want make a sort of "Favorites" page. 因此,我要创建一个“收藏夹”页面。 When you click on any item on the list, it writes it to a separate XML file within the app package. 当您单击列表上的任何项目时,它会将其写入应用程序包内的单独XML文件中。 After it writes to that XML, I need to make sure it still updates the list each time the app is loaded, instead of strictly saving the contents of that entry at the time it was written. 在将其写入XML之后,我需要确保每次加载应用程序时它仍会更新列表,而不是在编写该条目时严格保存该条目的内容。 What method would be the best way to go about this? 哪种方法是解决此问题的最佳方法?

First you get hold of the relevant IsolatedStorageFile using IsolateStorageFile.GetUserStoreForApplication() and then create a file using IsolatedStorageFile.CreateFile . 首先,使用IsolateStorageFile.GetUserStoreForApplication()掌握相关的IsolatedStorageFile ,然后使用IsolatedStorageFile.CreateFile创建一个文件。 That returns a Stream which you can write to in the normal way: 这将返回一个Stream ,您可以用常规方式写入它:

using (var storage = IsolateStorageFile.GetUserStoreForApplication())
using (var stream = storage.CreateFile("test.xml"))
{
    doc.Save(stream); // Where doc is an XDocument
}

It's as simple as that. 就这么简单。

One key point is to avoid thinking of IsolatedStorageFile as a single file - think of it as being a whole drive for your application. 一个关键点是避免将IsolatedStorageFile视为单个文件-将其视为应用程序的整体驱动器。 It can contain files, directories etc. It may end up being a single file in the native file system, but your application doesn't need to know or care about that. 它可以包含文件,目录等。它最终可能只是本机文件系统中的单个文件,但是您的应用程序不需要知道或关心它。

You could store the items to an XML file in the IsolatedStorage, just like you would do for any other file. 您可以将这些项目存储到IsolatedStorage中的XML文件中,就像处理其他任何文件一样。

When the application is loaded, you need to set the actions in the Application_Launching event handler (in App.xaml.cs) - for example, you could create a List that will be bound to a ListView element that will get contain elements from the loaded XML file. 加载应用程序时,您需要在Application_Launching事件处理程序(在App.xaml.cs中)中设置操作-例如,您可以创建一个List,该List将绑定到ListView元素,该元素将包含已加载的元素XML文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM