This project is read-only.

Shared attribute

May 29, 2013 at 7:20 AM
Edited May 29, 2013 at 7:21 AM
I'm having trouble getting the Shared attribute to work. Can you check this code and let me know if I'm doing it right?

In the file that defines my StorageService, I have:
 public interface IStorageProvider
  {
    Task<List<ObservationPlan>> LoadPlans();
    Task<ObservationPlan> GetPlan(string uniqueId);
  }

  [Shared(Permanent = true)]
  [Export(typeof(IStorageProvider))]
  public class StorageProvider : IStorageProvider
  {
    public List<ObservationPlan> Plans { get; set; }
...
In my MainViewModel and PlanDetailViewModel, I have this declaration:
    [Import]
    public IStorageProvider StorageProvider { get; set; }
In the MainViewModel, I call a function that loads the Plans property with a list; however, when I try to call a function that accesses the Plans property in the PlanDetailViewModel, the Plans property is null.

Can you see if there is a simple mistake that I'm making?

Thanks,
Chuck
May 29, 2013 at 1:20 PM
Hi Chuck,

Everything looks correct but it hard to tell without seeing the whole thing. One thing you can try is to put a constructor in StorageProvider and see if it's being called twice (this would indicate there is a problem with the injection).

You are welcome to zip up your project and send it to me if you want and I can take a look but from here that looks like a use case I do regularly.

-Ian
May 30, 2013 at 4:57 AM
I wasn't sure if I had the incantation right so I wasn't sure whether the problem was with the way I set up the attributes or something else in my code.

Once I knew that I had the attributes correct and stopped worrying about the new things I'm learning, the absolutely rookie mistake that I made in my code jumped out at me.

This:
    ObservableCollection<ObservationPlan> Plans = new ObservableCollection<ObservationPlan>();
should be this:
    Plans = new ObservableCollection<ObservationPlan>();
When I changed from a local variable to a property I forgot to get rid of the type declaration. I've got the problem fixed and am moving on. Thanks again for your help.

Chuck
May 30, 2013 at 2:12 PM
Good to hear, yeah it is a little bit of black magic how it all comes together but it's pretty cool :)

I'm starting a blog on StyleMVVM and one of the first entries I did was on the DI container. If you are interested it's located at http://stylemvvm.wordpress.com

Anyways feel free to contact me if you have any questions

-Ian
May 31, 2013 at 6:23 PM

Yes, thank you. I was able to get as far as I did by reading that section of your blog. Unfortunately, I’m still in the early stages of learning your toolkit, so it’s natural for me to assume that any problem that I’m having is due to not understanding the toolkit, or the way that it works.

The more that I work with it, the more I’m getting comfortable with style and idioms that you used and the fewer questions I’ll have over all.

Again, thanks for your quick responses to my questions – its making learning Style MVVM a pleasure instead of a chore.

Chuck