如何序列化Listbox和DataGrid? [英] How to Serialize a Listbox and DataGrid?

查看:99
本文介绍了如何序列化Listbox和DataGrid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



可以在WPF应用程序中序列化ListBox和DataGrid吗?

有人有一个例子吗?

谢谢

Hi,
It is possible to serialize a ListBox and DataGrid in WPF application?
Somebody has an example?
Thanks

推荐答案

不要序列化UI。在UI后面序列化一些数据模型



基本上,你应该有一个代表数据的单独层。它应该是一组纯数据类型,与UI无关。通常,将UI与应用程序的其他部分分开是非常重要的。



您应该拥有与数据绑定的代码UI。当你在谈论简单模型(而不是数据库)时,你要自己在文件中序列化/反序列化,很可能,你应该自己创建绑定。它可能只是来自数据实例的UI群体的过程(您需要重用,因为它可以是初始化数据或从持久性中读取的数据)以及更新UI更改数据的机制。



这样,序列化应该单独完成。你不应该自己做。相反,更好地使用数据合同

http: //msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ]。



这种方法最强大,非侵入性(你永远不会改变你的模型)序列化,只添加一些属性)并且最容易使用。请参阅我过去的答案,我主张这种方法:

如何在我的表单应用程序中使用XML文件写入器和阅读器? [ ^ ],

创建属性文件...... [ ^ ]。



现在,关于UI的隔离。这是一个非常重要的方面。我建议你学习和分析以下架构模式的适用性 http://en.wikipedia .org / wiki / Architectural_pattern_(computer_science) [ ^ ]):



MVVM - 模型视图模型,

http://en.wikipedia.org/wiki/Model_View_ViewModel [ ^ ],



MVC - 模型 - 视图 - 控制器,

http://en.wikipedia.org/wiki/Model-view-controller [ ^ ]),



MVA - 模型 - 视图 - 适配器,
http://en.wikipedia.org/wiki/Model-view-adapter [ ^ ],



MVP - 模型 - 视图 - 演示者,

http ://en.wikipedia.org/wiki/Model-view-presenter [ ^ ]。
注意这些架构的动机。如果您了解它,您将能够创造更好的设计理念。



-SA
Don't serialize UI. Serialize some data model behind UI.

Basically, you should have a separate layer which represents data. It should be a set of pure data types, not related to UI in any way. Generally, it is very important to separate UI from other parts of application.

You should have the code binding UI with the data. As you are talking about simple model (not database) which you are going to serialize/deserialize in a file by yourself, most likely, you should create binding by yourself. It could be just the procedure of population of UI from the instance of data (which you need to reuse, as it can be either initialized data or the data read from persistence) and the mechanism of updating data on UI changes.

This way, the serialization should be done separately. You should not do it by yourself. Instead, better use Data Contract:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

This approach is the most robust, non-intrusive (you never change your model to be serialized, only add some attributes) and easiest to use. Please see my past answers where I advocate this approach:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^].

Now, about isolation of UI. This is a very important aspect. I suggest you learn and analyze applicability of the following architectural patterns (http://en.wikipedia.org/wiki/Architectural_pattern_(computer_science)[^]):

MVVM — Model View View Model,
http://en.wikipedia.org/wiki/Model_View_ViewModel[^],

MVC — Model-View-Controller,
http://en.wikipedia.org/wiki/Model-view-controller[^]),

MVA — Model-View-Adapter,
http://en.wikipedia.org/wiki/Model–view–adapter[^],

MVP — Model-View-Presenter,
http://en.wikipedia.org/wiki/Model-view-presenter[^].
Pay attention for the motivation of those architectures. If you understand it, you would be able to create better design ideas.

—SA


这篇关于如何序列化Listbox和DataGrid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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