Windows phone 7 WP7在独立存储的不同行上保存多个条目
我从表单中获取信息,将其保存到独立的存储器中,并在一个单独的页面上建立一个不同条目的列表。我可以显示第一个数据项的文本,但就是不知道如何继续将它们存储在同一个文件中 这是我的表格页:Windows phone 7 WP7在独立存储的不同行上保存多个条目,windows-phone-7,isolatedstorage,Windows Phone 7,Isolatedstorage,我从表单中获取信息,将其保存到独立的存储器中,并在一个单独的页面上建立一个不同条目的列表。我可以显示第一个数据项的文本,但就是不知道如何继续将它们存储在同一个文件中 这是我的表格页: var multipleStorage = IsolatedStorageFile.GetUserStoreForApplication(); string multipleFile = "multipleFile.txt"; using (var file = mu
var multipleStorage = IsolatedStorageFile.GetUserStoreForApplication();
string multipleFile = "multipleFile.txt";
using (var file = multipleStorage.OpenFile(multipleFile, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
{
using (var writer = new StreamWriter(file))
{
writer.Write(nameTextBox.Text + ", " + dunsTextBox.Text + ", " + typeCheck + ", " + resellerCheck + System.Environment.NewLine);
}
}
这是我的接收页面:
private void resultTextBlock_Loaded(object sender, RoutedEventArgs e)
{
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (StreamReader sr = new StreamReader(store.OpenFile("multipleFile.txt", FileMode.Open, FileAccess.Read)))
{
resultTextBlock.Text = sr.ReadToEnd();
}
}
}
这并不是孤立存储的一个好用法。IsolatedStorage旨在在您退出应用程序后保存信息。因此,将信息保存到磁盘可能非常耗时 更好的方法是 1:。具有全局对象/类/等,例如 App.xaml.cs的对象类似于:
public static Dictionary<string,object> myPageContextObjects;
或者2:,您可以使用querystring方法。导航到新页面时,将信息添加到URI中。比如
NavigationService.Navigate(new URI("mypage.xaml" + "?nameTextBox.Text=" + nameTextBox.Text + "&dunsTextBox.Text=" + dunsTextBox.Text....) ).
在新页面上时,重载OnNavigatedTo方法以访问字符串
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
string selected = String.Empty;
//check to see if the selected parameter was passed.
if (NavigationContext.QueryString.ContainsKey("selected"))
{
//get the selected parameter off the query string from MainPage.
selected = NavigationContext.QueryString["selected"];
}
}
我在前面做了一个快速解决方案,演示了一个跨页面传递信息的简单示例。你可以在这里下载:
如果试图添加到文件中,则需要使用
System.IO.FileMode.Append
属性
var multipleStorage = IsolatedStorageFile.GetUserStoreForApplication();
string multipleFile = "multipleFile.txt";
using (var file = multipleStorage.OpenFile(multipleFile, System.IO.FileMode.Append, System.IO.FileAccess.Write))
{
using (var writer = new StreamWriter(file))
{
writer.Write(nameTextBox.Text + ", " + dunsTextBox.Text + ", " + typeCheck + ", " + resellerCheck + System.Environment.NewLine);
}
}
感谢您的回复,我理解这些其他传递数据的方法,但实际上我需要为我正在处理的任务演示独立存储。当然。好的,如果您不打算使用流,您可以这样做:我在页面之间传递数据并存储它,但是当我返回主页添加另一组信息时,它只会替换目标页面上的第一组信息。我正在尝试创建一个列表。/////Summary://打开文件(如果存在),并查找文件末尾,或者创建//一个新文件。System.IO.FileMode.Append只能与//System.IO.FileAccess.Write一起使用。尝试查找文件结尾//之前的位置将引发System.IO.IOException,任何读取尝试都将失败//并引发System.NotSupportedException。
var multipleStorage = IsolatedStorageFile.GetUserStoreForApplication();
string multipleFile = "multipleFile.txt";
using (var file = multipleStorage.OpenFile(multipleFile, System.IO.FileMode.Append, System.IO.FileAccess.Write))
{
using (var writer = new StreamWriter(file))
{
writer.Write(nameTextBox.Text + ", " + dunsTextBox.Text + ", " + typeCheck + ", " + resellerCheck + System.Environment.NewLine);
}
}