[UWP]图片库来自文本文件 [英] [UWP]Image Gallery from text file

查看:93
本文介绍了[UWP]图片库来自文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表框和一个图像...

I have a listbox and an image...

XAML

<ListBox Name="listbox">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}"></TextBlock>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
<Image Source="{Binding ElementName=listbox,Path=selectedItem."TEXT SECOND LINE HERE"}" Height="300"/>

C#

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);
                }

这将打开一个这样的文本文件:

This will open a text file like this:

--- CAT
http://server.com/cat.png

--- DOG
http://server.com/dog.png

--- BIRD
http://server.com/bird.png

...并仅使用动物填充列表框;如何获取文本文件的第二行以在所选列表项上显示正确的图像! ?

... and populate the listbox with just the animal; How can I get the second line of the text file to show the correct image on selected list item!?

任何帮助都会很棒;

提前致谢!!

推荐答案

您好

欢迎来到开发通用Windows应用程序
论坛!

请阅读粘贴帖子,特别是  发布指南:主题行
标签
 &absp; 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 , and don't forget add tag to your question.

可以直接访问文件的单行。例如,要访问第2行:

It is possible to access a single line of a file directly. For instance, to access line 2:

 	    FileOpenPicker picker = new FileOpenPicker();
            picker.ViewMode = PickerViewMode.Thumbnail;
            picker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
            picker.FileTypeFilter.Add(".txt");

            StorageFile file = await picker.PickSingleFileAsync();

            string d = (await FileIO.ReadLinesAsync(file)).Skip(1).Take(1).First();


这将只返回所需的行。

This will return only the line required


这篇关于[UWP]图片库来自文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆