I got a Silverlight page opening a dialog. It is an administration page with some advanced logic talking to a database over a webservice. For the Silverlight client side, we use MVVM to its fullest. Opening the dialog the first time, everything works fine according to the implemented logic.
The problem: The second time opening the dialog, our data bound setters start receiving the wrong values.
Code:
How the dialog is created:
MyPopupViewModel myPopup = new MyPopupViewModel();
Caliburn.Micro.Execute.OnUIThread(() => WindowManager.ShowDialog(myPopup));
One of the bindings that eventually get the wrong values:
<ComboBox
ItemsSource="{Binding YesNoItems}"
SelectedValue="{Binding IsSynchronizing, Mode=TwoWay, Converter={StaticResource BooleanToYesNoConverter}}"
/>
What I have tried:
-
I put breakpoints in the setters. This is how I realized the logic is correct but that the setter is called with other values the second time it is opened.
-
I found the getters being called {1, 2, 1} times the {1st, 2nd, 3rd} time they are opened.
-
I made sure the dialog is initialized each time. Maybe Caliburn-micro caches the old one somehow, but it apparently does not cache it for long, as it works fine again the third time it is opened.
Solved it by adding the following decorator in the constructor of the dialog
View, in the code-behind. I already had that decorator in theViewModel, but theViewapparently needed it too.A colleague helped me to find this out by breakpointing and then setting an ID for each instance. That way we proved there were several instances. Very practical. So the Views needed to be told to recreate each time instead of being reused.
The final code-behind looks like this: