private void CleanupViewModel(object context) { VstoContribLog.Debug(_ => _("Cleaning up viewmodel for context: {0}", context.ToLogFormat())); if (!contextToViewModelLookup.ContainsKey(context)) { VstoContribLog.Warn(_ => _("Cannot find ViewModel to cleanup: {0}", context.ToLogFormat())); return; } var viewModelInstance = contextToViewModelLookup[context]; VstoContribLog.Info(_ => _("ViewModel is {0}", viewModelInstance.ToLogFormat())); var notifyOfPropertyChanged = viewModelInstance as INotifyPropertyChanged; if (notifyOfPropertyChanged != null) { notifyOfPropertyChanged.PropertyChanged -= NotifiesOfPropertyChangedPropertyChanged; } viewModelInstance.Cleanup(); vstoContribContext.ViewModelFactory.Release(viewModelInstance); customTaskPaneRegister.CleanupViewModel(viewModelInstance); contextToViewModelLookup.Remove(context); }