存储 WPF TreeView 的文件信息 (C#)

发布于 2024-07-19 06:05:16 字数 432 浏览 10 评论 0原文

嘿。 我希望这个问题以前没有出现过 - 我在相关问题列表中找不到它。

不管怎样,我是一个相当新手的程序员(计算学位的第一年,所以我了解蹲下),并且我目前正在使用 C# 和 WPF。 为了好玩,我试图创建一个简单的音乐浏览器 - 首先,它只会读取我的目录并列出文件,然后在双击或其他任何操作时启动一个。 无论如何,我在如何存储 TreeViewItems 方面遇到了麻烦。 我需要将其存储为分层设计,这样我就可以循环遍历或其他什么,并在创建所有路径后填充树视图,但我不确定执行此操作的最佳方法。 Atm,我正在使用 TreeViewItem[][][] - 包含数组的数组的数组,这实际上似乎是错误的想法......

现在考虑一下,也许我应该创建一个数据库,并从中填充树视图? 唯一的问题是,我也从来没有做过这样的事情 XD。

谢谢你的帮助!

- 食品

Hey there. I hope this question hasn't come up before - I couldn't find it on the list of related questions.

Anyway, I'm a fairly newbie programmer (first year of a computing degree, so I know about squat), and I'm currently having a play with C# and WPF. For fun, I'm trying to create a simple music browser - to begin with, it'll just read my directory and list out the files, then launch one if you doubleclick or whatever. Anyway, I'm having trouble on how to store the TreeViewItems. I need to store it as a hierarchical design, so I can loop through or whatever and populate the Treeview once I've created all the paths, but I'm not sure of the best way to do this. Atm, I'm using TreeViewItem[][][] - an array of arrays that contain arrays, which actually seems wrong thinking about it...

thinking about it now, perhaps I should create a database, and populate the treeview from that? The only problem is, I've never done anything like that either XD.

Thanks for any help!

-Edibles

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

究竟谁懂我的在乎 2024-07-26 06:05:16

让我们退一步看一下整体架构。 在这种情况下,您通常会让 TreeView 显示数据、表示数据的类类型以及文件/数据库等。 作为数据的存储。

模式应该是从文件/数据库/等加载数据。 到表示实体的自定义类类型中。 接下来,您将该源绑定到 TreeView(使用 WPF 绑定或手动绑定)。

假设您希望树中的每个节点代表一张音乐专辑。 您将创建一个 MusicAlbum 类,该类可能由艺术家、专辑标题、专辑日期、歌曲列表等组成。此数据在文件/数据库/等之间进行序列化。

接下来,将该数据绑定到 TreeView。 WPF 支持直接绑定到自定义对象列表,或者您可以手动配置绑定。 如果您是开发新手,您可能需要考虑手动绑定,因为它更容易配置,并且会给您带来良好的实践。

就存储而言,我建议使用数据库(如 SQLite)或 XML 文件。 由于您正在学习开发,因此我建议您从 XML 文件开始。 这是一种表示数据的好方法,并且不需要像数据库那样复杂的开销。 如果您将其提升到生产级别,我肯定会推荐一个数据库。

编辑:问题本身有点模糊,因此您可能需要更具体地说明您的具体要求。 您想要 TreeView 绑定的代码示例还是需要指导或整体结构?

Let's take a step back and look at the overall architecture. In this scenario, you would typically have the TreeView display the data, a class type that represents the data, and the file/database/etc. as the storage for the data.

The pattern should be that you load the data from the file/database/etc. into the custom class type that represents the entity. Next, you would bind that source to the TreeView (either using WPF binding or manually binding).

Let's say you want each node in the tree to represent a music album. You would create a MusicAlbum class that is maybe composed of an Artist, album title, album date, SongList, etc. This data is serialized to and from a file/database/etc.

Next, you bind that data to the TreeView. WPF supports binding directly to lists of custom objects or you can manually configure the binding. If you are newer to development, you might want to consider manually binding since it is a little easier to configure, and it will give you good practice.

As far as the storage is concerned, I would recommend either a database (like SQLite) or an XML file. Since you are learning to develop, I recommend starting with an XML file. This is a good way of representing data and doesn't require the same overhead in complexity that a database does. If you were taking this to a production level, I would definitely recommend a database.

EDIT: The question itself is a little vague so you might want to be more specific as to what you are exactly asking for. Do you want code samples of TreeView binding or are you wanting guidance or overall structure?

风渺 2024-07-26 06:05:16

如果您的目的只是通过资源管理器样式的文件夹结构访问音乐,那么您不需要数据库,因为 Windows 文件系统已经为您提供了一个从中获取数据的集中点。 不过,您在启动时尝试填充整个树时确实要小心,因为您不希望用户等待应用程序爬行其硬盘驱动器的整个结构,然后才能开始导航树。 仅在需要时加载树项的过程称为延迟加载,超出了此处答案的范围。 幸运的是,这篇代码项目文章展示了如何做到这一点。

另一方面,如果您希望模仿 Windows Media 播放器库风格,其中用户提供特定目录,并且您创建了一种按艺术家、专辑等导航 MP3 的方法,那么您可能想要后台某种数据库。 在这些情况下,您需要从所选文件夹中的 mp3 中读取标签信息,将信息添加到良好规范化的数据库中(以允许快速搜索和交叉引用)并提供允许用户以这种方式导航的 UI。 您将再次寻求延迟加载树 - 选择一位艺术家,然后在请求时使用数据库中的所有专辑/歌曲填充子节点 - 但在到达那里之前还有很多工作需要完成(选择一种数据库类型,定义模式,学习如何解析 mp3 标签等),所以我假设您指的是第一个选项。

If your intention is to only access the music through an explorer style folder structure then you don't need a database since the windows filesystem already supplies you with a centralised point to get your data from. You do want to be careful about trying to populate the entire tree at startup though since you don't want the users waiting for the application to crawl the entire structure of their harddrive(s) before they can start navigating the tree. The process of only loading tree items when they are needed is called lazy loading, and is beyond a scope of an answer here. Fortunately this Code Project article shows how to do exactly this.

On the other hand, if you are looking to mimic the Windows Media player library style where the user supplies specific directories and you create a way to navigate the MP3s by artist, album etc. then you probably will want some kind of database in the background. In these cases you need to read the tag information from the mp3s in the selected folders, add the information into a nicely normalised database (to allow for fast searching and cross-referencing) and supply a UI that allows the user to navigate this way. Once again you'll be looking to lazy load the tree - select an artist then populate the child nodes with all their albums/songs from the database at the time of request - but there is a lot more work to complete before you get there (select a type of database, define the schema, learn how to parse the mp3 tags etc.) so I'll assume that you meant the first option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文