Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

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

Thanks in advance
Posted

1 solution

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
 
Share this answer
 
Comments
Silju MC 8-Aug-11 1:47am    
Thank you for spending your time Mr. SA. :-) This is a nice answer. My 5+
Sergey Alexandrovich Kryukov 8-Aug-11 1:56am    
You're welcome.
If you agree it makes sense, please accept the answer formally (green button) -- thanks.
--SA
Silju MC 8-Aug-11 1:51am    
Actually my problem is to save state of a control. Now I did this by storing the data of all controls. I thought that if there is a way to serialize the control object we can easily save the state and resume the state. :-) any way thank you again. :-)
Sergey Alexandrovich Kryukov 8-Aug-11 1:56am    
Same thing. You need to enter text to text box and restore it sometimes. The model includes some string; the UI population is assignment this string to the property Text; the update of data model is reading the property Test into model. The key is not to serialize controls -- keep UI away from data layer. You don't have to have only one data model -- could be different models, sub-models, etc. The link between model and UI could be controller in MVC, etc. See the patterns.
--SA
Silju MC 8-Aug-11 1:58am    
OK :) Thanks a lot.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900