Windows Phone应用程序中的序列化 [英] Serializing in windows phone application

查看:103
本文介绍了Windows Phone应用程序中的序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

TextBox不可序列化.有什么方法可以在Windows Phone应用程序中序列化TextBox/PasswordBox控件?

在此先感谢

Hi All,

TextBox is not serializable. Is there any way to serialize a TextBox/PasswordBox control in windows phone application?

Thanks in advance

推荐答案

不要序列化控件!序列化数据模型.

您需要有一个单独的数据层,并将其与UI控件绑定.用户界面将根据数据进行填充,并且数据将通过用户对用户界面的操作进行更新.

如果相对较小,则可以一次序列化整个数据层.最好的方法是使用数据合同System.Runtime.Serialization.DataContractSerializer.

参见:
http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. runtime.serialization.datacontractserializer.aspx [ ^ ].

这是一种非常健壮,易于使用且通用的方法.您不必手动使用XML.您要做的就是为类型和成员添加属性,例如[DataContract][DataMembers]来描述合同.

即使包含循环引用(即不是树,而是更通用的图),您也可以存储和还原整个对象图.

请在我倡导这种方法的地方查看我过去的解决方案:

如何在我的表单应用程序? [ ^ ],
创建属性文件... [ http://en .wikipedia.org/wiki/Architectural_pattern_(computer_science) [ ^ ]):

MVVM —模型视图视图模型,
http://en.wikipedia.org/wiki/Model_View_ViewModel [> 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 controls! Serialize data model.

You need to have a separate data layer and bind it with the UI controls. The UI will be populated based on data and the data will be updated by the user''s manipulation with the UI.

You can serialize the whole data layers at once, if it is relatively small. The best way to do it is using Data Contract and System.Runtime.Serialization.DataContractSerializer.

See:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx[^].

This is a very robust, easy-to-use and universal approach. You don''t have to work with XML manually; all you do is adding attributes to type and members, such as [DataContract] and [DataMembers] to describe a contract.

You can store and restore the whole object graph, even if it contains circular references (that is, not a tree but a more general graph).

Please see my past solutions where I advocate this approach:

How can I utilize XML File streamwriter and reader in my form application?[^],
Creating a property files...[^].

Also, 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


这篇关于Windows Phone应用程序中的序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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