[SOLUTION] See my Answer to this question.
I have two problems with child windows in my Silverlight 4 app. I created a pair of ChildWindows in my Silverlight app. The first child window has a button which causes the 2nd child window to be displayed.
Problem #1
When I run the app, make the first window come up, and then click the button in the first child window to display the 2nd window. If I click *either* the OK or Cancel button in the 2nd window, I get the following exception:
Unable to cast object of type 'System.EventArgs' to type 'System.Windows.RoutedEventArgs'.
This exception occurs on the one and only line of code in either method that does nothing more than set DialogResult to true or false. I have added NO OTHER CODE to the form (over and above the code added by the VS designer).
Visual Studio automagically added the following code when I created the window.
Private Sub OKButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles OKButton.Click
Me.DialogResult = True
End Sub
Private Sub OKCancel_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles OKButton.Click
Me.DialogResult = False
End Sub
I also tried removing those methods, and explicity adding a Click attribute to the XAML, and I got slightly different methods, but the same exception:
Private Sub OKButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Me.DialogResult = True
End Sub
Private Sub CancelButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Me.DialogResult = False
End Sub
Problem #2
When I bring up the first child window, and then dismiss it (with the OK button), it closes just fine. If I do it a 2nd time, it closes just fine, but the entire app appears to be disabled. I can't get it to reactivate, no matter what I do. While trying to determine why this is happening, I discovered that the
Closed
event is getting called twice.
To see if the same window was sending the event twice, I created a GUID for the window (in the constructor), and saw in the debugger that it is indeed sending the closed event twice, but it only handles the OK button click (in the form that's closing) one time.
EDIT #1 ==============
I created a small test app (using C# instead of VB) that creates/displays the first window, which contains a button that displays the 2nd window, and it works fine. For problem #2, I figured it's something I did, but problem #1 is the one that baffles me.
Edit #2 ==============
I created exactly the same simple app using VB, and problem #1 exists. It's a VB.Net thing.
EDIT #3 - 03/23 ==============
Here's the stack trace:
at Silverlight4App3.MainPage._Lambda$__2(Object a0, EventArgs a1)
at System.Windows.Controls.ChildWindow.OnClosed(EventArgs e)
at System.Windows.Controls.ChildWindow.Close()
at System.Windows.Controls.ChildWindow.set_DialogResult(Nullable`1 value)
at Silverlight4App3.ChildWindow1.OKButton_Click(Object sender, RoutedEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs,
Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
A guy that answered this question on the Silverlight MSDN froums, suggested that I make sure the templates used by the designer weren't corrupted, and my files matched his exactly.