Unregistering ViewModel

Topics: Prism v2 - Silverlight 4
Jan 6, 2011 at 9:08 AM

Hi,

We have implemented the framework Prism/Unity in our application.
We register the view and the viewmodel with register.type and we pass in parameter ContainerControlledLifetimeManager.
We only have one instance for each view and view model in the application.
We would like to unregister the view and the view model each time we recall the view but we still with the same instance, means, no reload of the data context and the view model.
Here is what we have tried to recreate a new one: 
- Remove the weak reference
- Teardown the view in the region
- Dispose the datacontext in the view
- Teardown the View
The result of these actions, everything works but we never pass again in the constructor of the view model, it's still instantiated.
How can we unregister the link between the view and the view model to recreate a new one?
If you want some parts of code, let us know.
Thank you for your help.
Regards
PS: we made an issue on the Unity Issue Tracker but we have been told to post on the Prism forums...
Developer
Jan 6, 2011 at 4:42 PM

Hi,

If I understood your scenario correctly, you need your ViewModel to be recreated each time the view is resolved. In order to achieve that, you need to register your ViewModel's type without specifying a ContainerControlledLifetimeManager, so that a new instance of the type is retrieved each time you resolve it through the Unity container.

A possibility for achieving your scenario using Region Navigation would be to set the view's DataContext to a new instance of the ViewModel in the OnNavigatedTo method of your view. In order to do so, you must make your view implement the INavigationAware interface. You can read more about this in the Navigation chapter of the Prism MSDN documentation.

Please let me know if I have misunderstood your scenario. In that case, it would be helpful if you could provide us with an in-depth explanation of your scenario, so that we could help you achieve your requirements.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Jan 28, 2011 at 9:06 AM
Edited Jan 28, 2011 at 9:34 AM

Hi,

Thanks for your quick reply.

We have tried your first solution, deleting the ContainerControlledLifetimeManager but it doesn't seem to be working correctly... The ViewModel is not created again... Do we miss something ?

We will now have a look to your second solution.

Regards

FYI, we are using Prism 2...

Developer
Jan 28, 2011 at 2:00 PM

Hi,

In order for the first approach I've mentioned to work, it's likely that the view in which you want your ViewModel to be recreated should also be registered without specifying a ContainerControlledLifetimeManager, since you've probably set the DataContext in the view's constructor. If you had specified your view as a singleton instance, the constructor will not be called when you resolve it again.

If you want to benefit from the Navigation approach, you could migrate to Prism v4. If you don't want to migrate to the latest version, one possibility would be to implement a similar approach to what I've mentioned in my second possibility on your own. That is to say, for example, raise an event when you need to recreate your ViewModel, and obtain a new instance of it in the handler of that event.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Jan 28, 2011 at 4:53 PM

Hi,

We already tried not to specify a ContainerControlledLifetimeManager when registering the view, but it's still not working...

We don't really know where does the problem come from in our project solution...

We keep looking to resolve this issue...

Thanks again for replying so quick !

Developer
Jan 28, 2011 at 4:58 PM

Hi,

One possible approach you could pursue to find out the cause of the issue you're experiencing would be to put a breakpoint both in the constructor of the View and in the constructor of the ViewModel, so as to check whether new instances of them are being created or not.

If you keep experiencing problems after trying out the aforementioned suggestions, you could provide us with a repro sample so that we can help you further.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Jan 31, 2011 at 9:27 AM

Thank you for the advice but we already tried to put a breakpoint in the constructor and new instances are not created...

We will keep looking further because a repro sample is not as easy to do with this project...

FYI, we have several modules and several regions. We are navigating between the views with a ribbon (Divelements.SandRibbon). A Ribbon is implemented in each module.

If you have any more idea please let us know. We will let you know if we find something helpful while keeping debugging...

 

Regards