0

So, i have several pages with several UserControls on each. One of the UCs presents on every page, and is taking info from DataService (which is used as info storage) in its constructor.

While switching between pages, UC's ViewModel constructor is not called, so it is not updating info from DataService. It is called once, during creation of very first page.

Any ideas? Should i just make a method, and call it from codebehind? Or can i somehow bind viewmodel's fields to DataService?

Vitalii Vasylenko
  • 4,776
  • 5
  • 40
  • 64
  • Is the constructor called when you first navigate to second page? It should be, because the UC is being created then, but when you navigate back to the first page, the page is cached, so the constructor is not called. OnNavigateTo is raised every time you navigate to the page, even if it's cached. – Igor Ralic Apr 12 '13 at 18:44
  • @igrali Edited question a bit. I mean, UC's ViewModel is called only once. Should i call UC.Refresh() from every page's OnNavigateTo() ? – Vitalii Vasylenko Apr 12 '13 at 18:47
  • ViewModel constructor being called once depends on where you're creating the VM. If you create it in the UC constructor or in XAML, it gets created once and than cached so when you go back to the old page, it's not recreated. If you want to refresh the view every time, you should consider calling some sort of refresh method from OnNavigatedTo. Be careful only that the refresh doesn't get called twice - both in constructor and in OnNavigatedTo – Igor Ralic Apr 12 '13 at 18:55
  • @igrali hm.. maybe i should make an UC event, and raise it once new page is loaded? i just want to keep it automated. – Vitalii Vasylenko Apr 12 '13 at 19:12
  • Whatever works for you! :) – Igor Ralic Apr 12 '13 at 19:13
  • @igrali Other idea was to have different IDs to create different instances of VM (i'm using mvvm-light's locator). But what about garbage collector? Did you try that? – Vitalii Vasylenko Apr 12 '13 at 19:16
  • Nope, haven't tried it, sorry! – Igor Ralic Apr 12 '13 at 19:17

1 Answers1

0

Ok, answer is pretty simple. Just need to pass new parameter while solving SimpleIoc.

SimpleIoc.Default.GetInstance<MainViewModel>((uidHolder++).ToString());

Reference (some info about mvvm-light's ioc) : how to use MVVMLight SimpleIoc?

Community
  • 1
  • 1
Vitalii Vasylenko
  • 4,776
  • 5
  • 40
  • 64