使用2个变量从文本文件创建ListBox项 [英] Create ListBox items from text file with 2 variables
问题描述
所以,我希望有任何帮助来填充一个显示网站名称的ListBox,如果点击它就转到特定网址。
So, I would like any help to populate a ListBox that is going to show a website name and if it's clicked go to a specific url.
这就是文本文件中的内容:
This is what's inside of the text file:
#first website
http://firstwebsite.com
#second website
http://secondwebsite.com
#third website
http://thirdwebsite.com
我可以读取文件并使用名称填充列表框,但不能使URL工作。
I can read the file and populate the listbox with the name, but cannot put the url working.
FileOpenPicker picker = new FileOpenPicker();
picker.ViewMode = PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
picker.FileTypeFilter.Add(".txt");
StorageFile file = await picker.PickSingleFileAsync();
if (file != null) {
var stream = await file.OpenAsync(FileAccessMode.Read);
using (StreamReader reader = new StreamReader(stream.AsStream()))
{
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
if (line.StartsWith("#") {
listbox.items.Add(line);
}
任何帮助都很棒。
谢谢
Any help is great.
Thanks
推荐答案
嗨Rpgccv,
Hi Rpgccv,
请阅读粘贴帖子,尤其是 指南
发布:主题行标签  ;和 Windows 10 SDK和工具的已知问题 。
Please read the sticky posts, especially the Guide to posting: subject line tags and Known Issues for Windows 10 SDK and Tools .
我们应该能够添加&SelectionTypeChanged事件ListBox,如果我们可以通过&Sebsp.SelectedItem属性选择哪个url。然后我们可以使用 Launcher.LaunchUriAsync 到
启动网址。
We should able to add SelectionChanged event of the ListBox, and in the event we can get which url is selected by the Selector.SelectedItem property. Then we can use Launcher.LaunchUriAsync to launch the url.
例如:
<ListBox Name="MyListBox" SelectionChanged="MyListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"></TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
背后的代码:
private ObservableCollection<string> urls;
public MainPage()
{
this.InitializeComponent();
urls = new ObservableCollection<string> { "http://firstwebsite.com", "http://secondwebsite.com", "http://thirdwebsite.com" };
MyListBox.ItemsSource = urls;
}
private async void MyListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var listBox = sender as ListBox;
var str = listBox.SelectedItem as string;
await Windows.System.Launcher.LaunchUriAsync(new Uri(str));
}
最好的问候,
Jayden Gu
Jayden Gu
这篇关于使用2个变量从文本文件创建ListBox项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!