gpt4 book ai didi

c# - 如何用外部文本文件填充文本框并在此之后向其中添加单词

转载 作者:太空宇宙 更新时间:2023-11-03 13:22:35 26 4
gpt4 key购买 nike

我正在尝试用外部文本文件中的数据填充 visual studio 2012 文本框,此后,当您执行该程序时,您应该能够将更改写入此文本框并将它们保存在那里,但我得到了错误,如屏幕截图所示。此外,我在虚拟机上运行 Windows 8!

[截图]:http://i.imgur.com/NkZU38C.png "截图

下面是填充文本框的代码:

private void Page_Loaded(object sender, RoutedEventArgs e)
{
LoadWords(@"Assets\AdminPageKS1words.txt");

}

async private void LoadWords(string filename)
{
var wordList = new List<String>();
// this method reads line separated words from a text file and populates a List object //
Windows.Storage.StorageFolder localFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
// begin the file read operation
try
{
// open and read in the word list into an object called words

StorageFile sampleFile = await localFolder.GetFileAsync(filename);
var words = await FileIO.ReadLinesAsync(sampleFile);
// add each word returned to a list of words declared
// globally as List wordList = new List();
foreach (var word in words)
{
wordList.Add(word);
}
List1.ItemsSource = wordList;
}
catch (Exception)
{
// handle any errors with reading the file
}

这里是保存按钮的代码:

async private void SaveButton_Click(object sender, RoutedEventArgs e)
{

// locate the local storage folder on the device
Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

// create a new text file in the local folder called “File.txt”
StorageFile sampleFile = await localFolder.CreateFileAsync("File.txt",CreationCollisionOption.ReplaceExisting);

// write text to the file just created – text comes from a textblock called wordlistBox
await FileIO.WriteTextAsync(sampleFile, List1.Text);

// display a message saying that file is saved.
messageLabel.Text = keystage + "File saved";

}

public string keystage { get; set; }

最佳答案

如错误消息所述,List1(TextBox)没有 ItemsSource 属性。它有一个 Text属性(property)。

但这不是唯一的问题。因为你的 wordList 对象是一个 IList。因此,您需要将其转换为纯字符串。

一个解决方案是这样做:

List1.Text = string.Join(Environment.NewLine, wordlist);

这会将所有行连接在一起,中间有换行符。

还要确保您的文本框配置正确 AcceptsReturn设置为 true 以便显示换行符。

关于c# - 如何用外部文本文件填充文本框并在此之后向其中添加单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23663500/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com