views:

65

answers:

1

I'm building an application on Phone 7 using Silverlight and need to maintain some state between pages. I was hoping to store an object in app.cs and access it from each page but I haven't been able to find any documentation on how to do this.

What is the best way to access the same object between many different pages?

Thanks for your time,

-- Henry

+1  A: 

Answered my own question. I can access objects in app.cs by simply doing:

App app = (App)Application.Current;
app.whateverMyObjectIsCalled

Is this a reasonable thing to do?

henry
This is reasonable for applications of moderate complexity.
AnthonyWJones
If you store your object as a static property, you don't need to cast Application.Current to App, you should just be able to access App.whateverMyObjectIsCalled directly.
Dr Herbie
You should be careful about cross thread access to such objects and be sure to lock or use monitor.enter/exit to avoid race conditions. You may also find issues accessing the object from some non-UI threads. In such cases you should look to execute such access on the UI thread.
Matt Lacey
You will also have some issues with unit testing if you are doing any, since you're relying on static objects to fill dependencies.
nlawalker