【问题标题】:WP7 saving multiple entries on different lines from Isolated StorageWP7 从独立存储中保存不同行上的多个条目
【发布时间】:2011-02-17 06:01:32
【问题描述】:

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

这是我的表单页面:

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

            }
        }
    }

【问题讨论】:

    标签: windows-phone-7 isolatedstorage


    【解决方案1】:

    IsolatedStorage 并不是一个很好的用法。隔离存储旨在在您退出应用程序后保存信息。因此,将信息保存到磁盘可能非常耗时。

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

    public static Dictionary<string,object> myPageContextObjects;
    

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

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

    或者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"];
            }
    }
    

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

    【讨论】:

    • 感谢您的回复,我了解这些其他传递数据的方法,但我实际上需要为我正在处理的任务演示隔离存储。
    • 当然。好吧,如果你没有设置使用流,你可以这样做:
    • 我在页面之间传递数据并存储它,但是当我回去在主页上添加另一组信息时,它只是替换了目标页面上的第一组信息。我正在尝试创建一个列表。
    • // // 摘要: // 如果文件存在,则打开文件并查找文件末尾,或创建 // 一个新文件。 System.IO.FileMode.Append 只能与 // System.IO.FileAccess.Write 结合使用。尝试在文件末尾之前寻找一个位置 // 将引发 System.IO.IOException 并且任何读取尝试都会失败 // 并引发 System.NotSupportedException。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-27
      相关资源
      最近更新 更多