********有用!但是你必须在任何显示之前输入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"); }
用于在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 = savedTickets; }
Shaharyar.. 8
你的代码非常好,唯一的问题是它没有被执行.当您单击时,button
您的文件已创建.但是你没有输入任何东西,textbox
所以你永远不会读取文件.
我想你想在写完之后立即阅读它.将您的Read
文件代码放在代码后面Write
:
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"); //read file string savedTickets = await Windows.Storage.FileIO.ReadTextAsync(ticketsFile); viewTickets.Text = savedTickets; }
并删除viewTickets_TextChanged
事件处理程序.
你的代码非常好,唯一的问题是它没有被执行.当您单击时,button
您的文件已创建.但是你没有输入任何东西,textbox
所以你永远不会读取文件.
我想你想在写完之后立即阅读它.将您的Read
文件代码放在代码后面Write
:
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"); //read file string savedTickets = await Windows.Storage.FileIO.ReadTextAsync(ticketsFile); viewTickets.Text = savedTickets; }
并删除viewTickets_TextChanged
事件处理程序.