WP7在隔离存储的不同行上保存多个条目

时间:2011-02-17 06:01:33

标签: windows-phone-7 isolatedstorage

我从表单中获取信息,将其保存到独立存储并在单独的页面上构建不同条目的列表。我可以显示第一个数据条目的文本,但根本无法弄清楚如何继续将它们存储在同一个文件中。

这是我的表单页面:

        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();

            }
        }
    }

2 个答案:

答案 0 :(得分:1)

这对IsolatedStorage来说并不是很好用。 IsolatedStorage旨在让您在退出应用程序后保存信息。因此,将信息保存到磁盘可能非常耗时。

更好的方法是做到这一点 1:。拥有全局对象/类/等。如在 App.xaml.cs有一个像这样的对象:

public static Dictionary<string,object> myPageContextObjects;

并在您的页面上添加您需要传递的项目:

App.myPageContextObjects.Add("nameTextBox.Text",nameTextBox.Text);
...

2:,您可以使用查询字符串方法。导航到新页面时,将信息添加到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"];
        }
}

我之前做了一个快速解决方案,演示了一个跨页面传递信息的简单示例。您可以在这里下载: http://dl.dropbox.com/u/129101/Panorama_querystring.zip

答案 1 :(得分:1)

如果您尝试添加到该文件,则需要使用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);
        }
    }