开发者

WP7 saving multiple entries on different lines from Isolated Storage

I am taking information from a form, saving it to isolated storage and building a list of the different entries on a separate page. I can display the text of the first data entry but simply can't figure out how to continue to store them in the same file.

This is my Form Page:

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

And this is my receiving page:

    private void resultTextBlock_Loaded(object sender, RoutedEventArgs e)
    {
        using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (StreamReader sr = new StreamReader(store.O开发者_如何学运维penFile("multipleFile.txt", FileMode.Open, FileAccess.Read)))
            {
                resultTextBlock.Text = sr.ReadToEnd();

            }
        }
    }


This isn't really a good usage of IsolatedStorage. IsolatedStorage is designed to have information saved after you've exited the app. As such, saving information to disk can be very time-consuming.

A better way to do this would be 1:. Have a global object/class/etc. Such as in App.xaml.cs have a object like:

public static Dictionary<string,object> myPageContextObjects;

and on your page, add the items you need to pass:

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

Or 2:, you can use the querystring method. When navigating to anew page, add the info into the URI. Such as

NavigationService.Navigate(new URI("mypage.xaml" + "?nameTextBox.Text=" + nameTextBox.Text + "&dunsTextBox.Text=" + dunsTextBox.Text....) ).

When you're on the new page, overload the OnNavigatedTo Method to access the string.

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

I made a quick solution earlier that demonstrates a simple example of passing information across pages. You can download it here: http://dl.dropbox.com/u/129101/Panorama_querystring.zip


If you're trying to add to the file, you need to use the System.IO.FileMode.Append property.

 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);
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜