Multiple calls to InitializeVM on a shared VM

Jun 19, 2012 at 1:15 PM

I have an application with view/viewmodel stored in different xap files. I used the xap router to inform jounce of that.

The views are placed in a region with an ItemsControl and activated one at a time with a ViewNavigationArgs call and use of visual states in the views to show/hide them. Views are activated with a click on a link in a menu.

First click the view and view model are created correctly after the downloading of the xap.

Second click is the same with view/viewmodel from a second xap. The view replaced the first one, all is ok.

I click on the fiorst link : normally, the view/viewmodel are already created and initialized... Sometimes, I see with the trace messages that the InitializeVM of the ViewModel is called a second time! It's not a non-share VM...

After some clicks on differents links, I have no more unwanted calls to InitializeVM.

I protected that with a boolean flag in my VM, but when the problem arises, the view is not activated and the ItemsControl stays empty.

I hope anyone can help me and have an idea.

Thanks in advance,

Bruno

 

Coordinator
Jun 23, 2012 at 1:54 PM

It's tough to troubleshoot with so little information but it seems somehow maybe another copy is being activated. I would debug the application and create an object id (to do this, go into the Watch window, right click on the view model, and select "Make Object ID." That will tag the view model with a unique identifier like 1#, 2#, etc. Then, when you get the second call to the Initialize method, check the object ID. If it is different it means you have two instances. If it is not, then backtrack and go into the view model router to see why it's calling it twice - there is an internal collection of view models that should be unique and distinct, as it only calls the initialize method the first time it adds to the collection.

Jul 24, 2012 at 8:57 PM

Hello Jeremy,

First, thanks for your answer ! I just had a period of holidays and I come back with Silverlight and Jounce J

The view model is the same : the object ID is always #1. I investigate as suggested in the view model router (ViewModelRouter.cs) and, in the second call, the viewModelInfo.IsValueCreated is null. If I try a third time and others after!), it is always correct.

In the function in the line “var viewModelInfo = GetViewModelInfoForView(viewName);”, there is a linq statement which read the VMinfo from the _fluentRoutes, but the info is not correct in the second call. I don’t find where the Value/ IsValueCreated properties are initialized… maybe in the Value getter ?

Are these informations giving you an idea for where to serach for me?

Thanks in advance,

Bruno

De : jeremylikness [email removed]
Envoyé : lundi 25 juin 2012 22:08
À : bruno@deluard.com
Objet : Re: Multiple calls to InitializeVM on a shared VM [Jounce:360138]

From: jeremylikness

It's tough to troubleshoot with so little information but it seems somehow maybe another copy is being activated. I would debug the application and create an object id (to do this, go into the Watch window, right click on the view model, and select "Make Object ID." That will tag the view model with a unique identifier like 1#, 2#, etc. Then, when you get the second call to the Initialize method, check the object ID. If it is different it means you have two instances. If it is not, then backtrack and go into the view model router to see why it's calling it twice - there is an internal collection of view models that should be unique and distinct, as it only calls the initialize method the first time it adds to the collection.

Jul 24, 2012 at 10:12 PM

Hi Bruno,

Do you have the reference to Jounce marked as 'Copy Local' = false in all projects except the Shell?  That can give similar sorts of 'odd' behaviour.

Jul 27, 2012 at 1:16 PM

Hello,

Yes, I verified twice that this option is correctly configured.

Bruno