StringWriter in windows universal app

我们两清 提交于 2019-12-23 05:12:10

问题


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...


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/38904657/stringwriter-in-windows-universal-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!