RCairns
RCairns

Reputation: 155

Windows 8 XAML - Storing Objects local resource

I am still relatively new to development for Windows Store Apps in XAML/C# and I'm currently dealing with a very random and intermittent problem with an app I have written.

Firstly a quick overview of how my app works - user logs on once a day, downloads data from web service and stores the data in xml files. Each time the app opens/resumes the data is loaded from xml, deserialized and stored in memory in the Application.Resouces Resource Dictionary.

The objects I am storing are my own classes which contain Observable Collections of other classes. I have declared these in App.xaml

<localdata:MyClass x:Key="MyClassResource">

When a page needs this data I reference it using

MyClass myClass = (MyClass)App.Current.Resources["MyClassResource"];

and bind it to controls. The user updates the data and these changes would also be saved to file periodically.

I am now starting to doubt whether this is the correct approach for storing my data.

Every so often the users reports problems with the stored data - I don't have enough details to fully discuss the specific problem right now but I wanted advise on whether it is fine to store my own objects in the Application Resource Dictionary.

Upvotes: 0

Views: 59

Answers (1)

Silverstein
Silverstein

Reputation: 96

There's nothing wrong with your approach. It is actually a very common way to create and access the viewmodel. There is an excellent blog post by Paul Stovell describing different approaches to create and access the viewmodel.

  1. Create viewmodel from code-behind within the view
  2. Inject the viewmodel as dependency into the view
  3. Assign viewmodel to view's DataContext property
  4. Set viewmodel via XAML to DataContext property
  5. Define viewmodel as resource in XAML
  6. Use a view model locator in XAML
  7. DataTemplate property in XAML
  8. DataTemplate and view class in XAML

The referenced article describes all 8 approaches with examples. Your approach is number 5.

Upvotes: 0

Related Questions