开发者

Silverlight: Where to store data used across pages

I have a Silverlight application that consists of many pages that uses Navigation Framework. What is the ideal place to store data that should be accessed across all pages (XAMLs) and throughout开发者_JAVA百科 the lifetime of the application.

EDIT: Forgot to mention that I am currently doing it as a static class


Static members are generally a bad idea. You have no control over lifespan or ability to easily substitute another set of data (and don't get me started on the inability to do proper unit testing). You want to use some type of shared View Model/Data model.

If you are not going the whole PRISM route (we always use PRISM now for Silverlight and WPF), or Unity, or even just MVVM, then use simple singleton accessors on your data object.

There are lots of discussions over the best patterns for C# singletons, but you can learn a lot here http://www.yoda.arachsys.com/csharp/singleton.html

Hope this helps.


I like to create a class called Session, with a static property like this public static Session Default {get {return App.Current.Resources["Session"] as Session;}}, then create a new instance of it in app.xaml like this <classes:Session x:Name="Session"/>, now you can access it in code behind with Session.Default... and you can bind to it with Source binding and it will always be the same instance. I have expanded this pattern to a more complex and flexible pattern with base classes etc but that should suffice for your purposes. I wrote this code in this web window, it might not compile, if you need more help just let me know

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜