gpt4 book ai didi

c# - 如何将列表中的字符串显示到文本 block ?

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

我想在 Ui(xaml) 中显示文本文件中的文本,我已经将其拆分为列表到文本 block 。我使用数据绑定(bind)但它总是错误并且文本不出现所以你有什么方法可以指导我吗?谢谢,我在通用应用程序中做到了

private async void button_click(object sender, RoutedEventArgs e)
{
string content = "00:00:00;00:00:10;hello\r\n00:00:10;00:00:20;hi";
string filename = "test.txt";
// saves the string 'content' to a file 'filename' in the app's local storage folder
byte[] fileBytes = System.Text.Encoding.UTF8.GetBytes(content.ToCharArray());

// create a file with the given filename in the local folder; replace any existing file with the same name
StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);

// write the char array created from the content string into the file
using (var stream = await file.OpenStreamForWriteAsync())
{
stream.Write(fileBytes, 0, fileBytes.Length);
}

StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
Stream streams = await local.OpenStreamForReadAsync(filename);

string text = "";
List<string> textread = new List<string>();
using (StreamReader reader = new StreamReader(streams))
{
while (text != null)
{
text = reader.ReadLine();
if (text != null)
textread.Add(text);
}
}

foreach (string stringoutput in textread)
{
string[] words = stringoutput.Split(';');

datas.Add(new Datum { start = words[0], end = words[1], comment = words[2] });
}

foreach (Datum temp in datas)
{
string a = temp.start;
string b = temp.end;
string c = temp.comment;
}

}

public class Datum
{
public Datum Data;
public string start { get; set; }
public string end { get; set; }
public string comment { get; set; }
}

public class ViewModel
{
public ViewModel()
{
// data
var _Data = Enumerable.Range(1, 20)
.Select(x => x => (string)(x + 'a'));
Data = new ObservableCollection<Datum>(_Data);
}

public ObservableCollection<Datum> Data { get; private set; }
}

这是我的 XAML 的一部分:

        <ItemsControl ItemsSource="{Binding Data}">
<TextBlock Text="{Binding start}" />
</ItemControl>

最佳答案

您的 ItemsControl 需要一个包含文本 block 的 ItemTemplate:

<ItemsControl ItemsSource="{Binding Data}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding start}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

或者,如果您只想显示字符串 start,则可以使用 DisplayMemberPath:

  <ItemsControl ItemsSource="{Binding Data}" DisplayMemberPath="start" />

关于c# - 如何将列表中的字符串显示到文本 block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31471573/

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