Windows Universal应用程序中的StringWriter [英] StringWriter in windows universal app

查看:103
本文介绍了Windows Universal应用程序中的StringWriter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绝对是编程新手.

我正在尝试使用三个TextBox和一个最终的TextBlock创建一个非常简单的应用程序.我想将来自这三个TextBox的输入保存在本地,以便可以读取并在页面底部的TextBlock中将其显示为一个字符串.

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.

我已经阅读到StreamWriter类是我应该使用的类.我只是不知道从哪里开始...

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

如何存储每个TextBox的输入,并在页面底部的TextBlock中以一个字符串读取和显示该输入?

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

推荐答案

对于UWP,我们将数据保存在本地文件夹中的ApplicationData中.我们将用StreamWriter编写一个文件流,并用StreamReader读取同一流.

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.

去阅读有关 ApplicationData

首先创建一个类,以捕获您需要从UI声明的所有变量.

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

现在,我们可以将用户界面中的内容提交到applicationData存储.

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

现在让我们开始从存储中读取写入的数据.现在,我们将使用Stream Reader.

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.

这篇关于Windows Universal应用程序中的StringWriter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆