Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm looking for a way to load data into properties/variables etc and have this information accessible to all the pages of my app. I want the information to be loaded via a background thread to keep UI thread free. Some of the pages will have various properties of their controls binding to these global properties.

Here is what I tried.

  • Created a static class. All pages could access the data but can't bind.
  • Changed the static class to a Singleton and used DependencyProperty's. All pages could access data and binding worked fine but cross-threading issues when accessing via background threads.

I have read in various places on this subject but haven't really come up with the best method yet for my situation.

share|improve this question
add comment

2 Answers

I would probably have each page bind to different ViewModel objects, and let your ViewModels worry about accessing/updating the data in your "static" class.

If that's not an option, remember that DependencyObject is not the only way to go if you want to bind a page to an object. I personally prefer using INotifyPropertyChanged. This way you can have the UI update data on the UI thread and still be able read data on a different thread. Of course you're going to need to write your code in such a way that it is thread safe, but at least you won't get an exception when accessing properties that your UI is bound to.

Also note that singletons have their downsides. If I were you, I'd probably use dependency injection as an alternative if possible.

share|improve this answer
add comment

You can't update the UI directly with a background thread in Xaml-based applications. All UI updates need to be done via the Dispatcher on the UI thread.

To ensure that you are using the Dispatcher thread in a WP7 application (which is really just Silverlight) you would do something like:

Deployment.Current.Dispatcher.BeginInvoke( () => this.Detail = _staticObject.Detail);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.