简体   繁体   中英

StringWriter in windows universal app

I am absolutely new to programming.

I'm trying to create a very simple app with three TextBoxes and one final TextBlock. I want to save the input from these three TextBoxes locally so I can read and display it in a TextBlock on the bottom of the page in one string.

屏幕截图

I have read that the StreamWriter class is what I should be using. I just don't know where to start...

There's basically not a lot of code so far really

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();

        string name = tbxName.Text;
        string age = tbxAge.Text;
        string dinner = tbxDinner.Text;

        string[] answer = { name, age, dinner };

        StringWriter saveText = new StringWriter();
        StringReader readText = new StringReader(answer.ToString());
    }
}

How do I store the input of each TextBox and read and display that input in one string in my TextBlock at the bottom of the page?

I want the app to save and to be able to display the input after the app has been closed and reopened again.

Sorry, I know this is super simple stuff...

For UWP, we are going to save the our data in ApplicationData in the localfolder. We will the StreamWriter to write a filestream and StreamReader to read from the same stream.

Go and read more about ApplicationData

First create a class to capture all the variables you need declared from the UI.

public class Entity
{
    public string name { get; set; }
    public int age { get; set; }
    public string dinner { get; set; }
}

Now lets get to submitting to contents from the UI to the applicationData storage.

using System.IO
OnClickEvent()
{
    Entity ent = new Entity();
    ent.name = tbxName.Text;
    ent.age = int.Parse(tbxAge.Text);
    ent.dinner = tbxDinner.Text;

    //save
        var myStream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync("[PUT-HERE-NAME].txt", CreationCollisionOption.ReplaceExisting);

        using (StreamWriter writer = new StreamWriter(myStream))
        {
            var results = JsonConvert.SerializeObject(ent);

            writer.Write(results);
        }
}

Lets now work on reading the written data from storage. We are now going to use Stream Reader.

using System.IO;
using Newtonsoft.Json;
OnClickEvent
{
    Entity ent = new Entity();
    string content = string.Empty;
        var localFolder = ApplicationData.Current.LocalFolder;
        await localFolder.CreateFileAsync("test.txt", CreationCollisionOption.OpenIfExists);
        var stream = await localFolder.OpenStreamForReadAsync("test.txt");
        using (StreamReader reader = new StreamReader(stream))
        {
            content = await reader.ReadToEndAsync();

            ent= JsonConvert.DeserializeObject<Entity>(content);

        }
}

After being able to read, you can display the data to the user now.

tbxName.Text = ent.name;
tbxAge.Text = ent.age.ToString();
tbxDinner.Text = ent.dinner

Feel free to ask if you ran to any problem or dont understand something.

I want to save the input from these three TextBoxes locally

locally, you means in a file? If yes, you can save one array item per line, to do so:

System.IO.File.WriteAllLines("textfile.txt", answer);

and then, at other moment, you can create the answer again:

string[] answer = System.IO.File.ReadAllLines("textfile.txt");

Edit: Since you are developing a phone app, its better to use StorageFile :

StorageFolder folder =
    Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile sampleFile =
    await folder.CreateFileAsync("test.txt", CreationCollisionOption.ReplaceExisting);
await Windows.Storage.FileIO.WriteTextAsync(sampleFile, string.Join(";", answer));

This will save answer in a file with data separated by ;

To read back:

string text = await Windows.Storage.FileIO.ReadTextAsync(sampleFile);
string answer = text.Split(';');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM