Closed cirrusone closed 3 years ago
Just discovered I have to manually set .IsActive = true
on the viemodel.
Just discovered I have to manually set
.IsActive = true
on the viemodel.
@Sergio0694 what's the reason for requiring this? (Why does this default to false?) It seems like an issue that could catch a lot of developers out.
@mrlacey The rationale was to decouple the viewmodel instantiation with the actual initialization/reegistration, to offer more flexibility to consumers. For instance, consider a scenario where a developer initializes a list of child viewmodels that is then bound to a control like a Pivot
(or a TabView
, etc.). This allows you to only set each viewmodel as active when it's actually brought into view, and then disable it once it's removed from the view. It's a way to more easily connect the activation state to whether or not the viewmodel is actually displayed and in use. Makes sense? 🙂
I apologize as this was actually documented in the docs and code so was a case of rtfm. A comment on the code sample may have helped a little on this link https://github.com/windows-toolkit/MVVM-Samples/blob/master/docs/mvvm/ObservableRecipient.md
Now I know about this I think it is quite useful as I can initialize from the constructor first if required.
I've been testing
ObservableRecipient
in aWPF
application and want to useOnActivated
andOnDeactivated
explicitly. However it's not working for me, I've produced basic simple reproducible project and it's still not working,OnActivated
andOnDeactivated
are not firing. Am I missing something obvious or is this a bug?WpfApp1.csproj:
App.xaml:
App.xaml.cs:
MainViewModel.cs:
MessageForMainViewModel.cs:
MainWindow.xaml:
MainWindow.xaml.cs: