我必须在会话之间存储控件的内容。这就是我所做的:
private void Window_Closing(object sender, CancelEventArgs e)
{
IsolatedStorageFile f = IsolatedStorageFile.GetUserStoreForAssembly();
using(IsolatedStorageFileStream stream = new IsolatedStorageFileStream("somefilename", FileMode.Create, f))
using(StreamWriter writer = new StreamWriter(stream))
{
foreach(string path in searchFoldersListView.Items)
{
writer.WriteLine(path);
}
}
}
这是附加到Closing
窗口的事件。调试时,控件遍历所有项目,但是当我再次运行程序时,控件为空。事实证明,当我导航到该文件时,它是空的。当我通过将 a 放入 foreach 循环中更改代码以保存到常规文件File.AppendAllText()
时,该文件保存得很好。这是为什么?
编辑:阅读事件处理程序:
private void Window_Initialized(object sender, EventArgs e)
{
IsolatedStorageFile f = IsolatedStorageFile.GetUserStoreForAssembly();
using(IsolatedStorageFileStream stream = new IsolatedStorageFileStream("somefilename", FileMode.OpenOrCreate, f))
using(StreamReader reader = new StreamReader(stream))
{
string line = reader.ReadLine();
while(line != null)
{
searchFoldersListView.Items.Add(line);
}
}
}