********有用!但是你必须在任何显示之前输入TextBox.我猜那是因为我使用了'TextChanged'事件处理程序.如果我希望它在没有用户交互的情况下显示文本文件的内容,我会使用哪个事件处理程序?********
因此,当按下按钮时,我想将一些数据写入C#Windows 10通用平台应用程序中的文本文件,并且我希望TextBlock或TextBox在我的应用程序中读取该文本文件的内容.
我正在使用一个pivot样式的应用程序,在一个数据透视表上写入文件的按钮和我想要包含文本文件内容的TextBlock或TextBox在另一个数据透视表上.
我的代码如下.它不起作用.我不确定它是否创建和编写文件,我的TextBox或TextBlock都没有.:(
我从这里得到了代码:https://msdn.microsoft.com/en-us/library/windows/apps/mt185401.aspx
编写文件的代码:
private async void submitButton_Click(object sender, RoutedEventArgs e)
{
//WRITE THE TICKET TO A LOCAL DATABASE (txt)//
//Create the text file to hold the data
Windows.Storage.StorageFolder storageFolder =
Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFile ticketsFile =
await storageFolder.CreateFileAsync("tickets.txt",
Windows.Storage.CreationCollisionOption.ReplaceExisting);
//Write data to the file
await Windows.Storage.FileIO.WriteTextAsync(ticketsFile, "Swift as a shadow");
}
Run Code Online (Sandbox Code Playgroud)
用于在TextBox中读取文件的代码:
private async void viewTickets_TextChanged(object sender, TextChangedEventArgs e)
{
Windows.Storage.StorageFolder storageFolder =
Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFile ticketsFile =
await storageFolder.GetFileAsync("tickets.txt");
string savedTickets = await Windows.Storage.FileIO.ReadTextAsync(ticketsFile);
viewTickets.Text …Run Code Online (Sandbox Code Playgroud) 我还问的另一个通用平台应用程序问题,还在学习中!
快速问题-我需要使用哪种方法将数据写入文本文件以将字符串追加(而不是覆盖)文本文件?目前,我正在使用异步异步流,但它正在覆盖我的文件。以下是创建和写入文本文件的不同方法:https : //msdn.microsoft.com/zh-cn/library/windows/apps/mt185401.aspx
目前,这是我的代码。是否需要修改以将数据附加到文本文件而不是覆盖它,还是需要使用其他方法之一来附加数据?
//WRITE
//Create the text file to hold the data
Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFile ticketsFile = await storageFolder.CreateFileAsync("tickets.txt", Windows.Storage.CreationCollisionOption.OpenIfExists);
//Use stream to write to the file
var stream = await ticketsFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
using (var outputStream = stream.GetOutputStreamAt(0))
{
using (var dataWriter = new Windows.Storage.Streams.DataWriter(outputStream))
{
dataWriter.WriteString("Submitted: " + submitDate + "\n" + "Problem: " + problem + "\n" + "Room: " + location + "\n" + "Description: " + descriptionText.Text + "\n" + "Priority: " …Run Code Online (Sandbox Code Playgroud)