UWP Application (Prism.Unity NuGetPackage 6.3.0)
When navigating to the same page several times, a new instance of its view model is created and the old ones are held in memory and are not disposed.
This causes crashes because global events are fired several times using an event aggregator, also received by old ViewModels listening to it.
We're using the NavigationService for navigating through pages. Our pages and usercontrols are bound to viewmodels with prismMvvm:ViewModelLocator.AutoWireViewModel="True"
in XAML.
We've seen some threads about similar problems and the solution was to add a region behaviour using Regions. However, as far as I know, Prism UWP doesn't support regions in its current release.
We think the problem is related to the ViewModelLocator and NavigationService, because registering the viewmodels with Container.RegisterType and with a different LifetimeManager has no effect.
A crashing sample can be downloaded from GitHub: App1.zip
Repro:
- Execute application
- Navigate several times between Test1 and Test2
- Hit "RaiseEvent" for executing a global event
- All instances logging their hash code
- App should crash at some point