持久化数据绑定 WPF ObservableCollection 的首选方法?
我有一个继承自 ObservableCollection(Of MyObject) 的类,并且 MyObject 类按其应有的方式处理 INotifyPropertyChanged。 当用户通过绑定控件更新此数据时,集合将按预期更改。
我在表单上有一个“保存”按钮,旨在通过对每个项目调用 MyObject.Save 将该集合作为 XML 持久保存到磁盘。 这个逻辑是我写的。
我的问题是,这是持久化对象的首选方式,还是我缺少一个属性或接口来告诉 WPF 数据绑定/.Net 框架使用哪种方法来保存/加载数据?
我之所以这么问,是因为我见过的 WPF 示例省略了真实数据的保存和加载,所以我不确定。
I have a class that inherits from ObservableCollection(Of MyObject) and the MyObject class handles INotifyPropertyChanged as it should. When the user updates this data through bound controls the collection will change as expected.
I have a 'Save' button on the form which is meant to persist this collection to disk as XML by calling MyObject.Save on each item. This logic has been written by me.
My question is, is this the preferred way of persisting objects or is there an attribute or interface I am missing that would tell the WPF databinding / .Net framework which method to use to save / load the data?
I ask because the WPF samples I have seen omit the saving and loading of real data so I am not sure.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不希望 WPF 提供超出 .Net 核心提供的任何数据加载/保存功能,例如标准序列化机制。
如果您当前的方法符合您的需求,那么它看起来不错。
I would not expect WPF to provide any data loading/saving capability beyond that provided by the .Net core, such as the standard serialization mechanisms.
You current approach seems fine if it fits your needs.