I'm working on a WPF project with MVVM Light. This application runs on the system tray and generates a Windows with a ViewModel associated.
When the user closes Windows, I have a ViewModelLocator to release his instance ViewModel associated. There are multi-instances ViewModel at the same time, I can't remove all instance, just the one associated with closed Window
public class ViewModelLocator
{
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
SimpleIoc.Default.Register<CollectorViewModel>();
public static T GetNewInstance<T>(string uniqueKey)
{
var model = ServiceLocator.Current.GetInstance<T>(uniqueKey);
return model;
}
public static T GetInstance<T>()
{
return ServiceLocator.Current.GetInstance<T>();
}
public static void Cleanup(string uniqueKey)
{
SimpleIoc.Default.Unregister(uniqueKey);
}
}
public class CollectorViewModel
{
~CollectorViewModel()
{
//relase some resources
}
}
I tried to release the instance by this code. I'm sure that I passed correct uniqueKey for the instance
SimpleIoc.Default.Unregister(uniqueKey);
2nd try doesn't have the effect.
SimpleIoc.Default.Unregister<CollectorViewModel>(myInstance);
But when I check, it's always there.
SimpleIoc.Default.GetAllInstances(typeof(CollectorViewModel));
The question: How to release the instance?