Click here to Skip to main content
15,941,894 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
[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.
Updated 23-Mar-11 8:21am
wizardzz 21-Mar-11 16:51pm    
How does the CIL code vary between the C# and the VB projects?
DaveAuld 21-Mar-11 17:14pm    
Hi John, I created a test SL4 Nav app (with VB), on the MainPage a single button calls a Child Win, On the child Win a single button calls a second child win, As each shild window is opened, the main window goes slightly darker as expected, Click the ok or cancel or x in each window return to the previous window without issue, and can enter and leave as many times as i want, and the main page is always returned to normal and active once both the child windows are closed. No Exceptions raised and no locking of the main window.....oh, and I am not SP1 either.
AspDotNetDev 21-Mar-11 18:33pm    
I was not able to reproduce the problem (though I have SP1 installed). Can you post the contents of the following files (your names may be different, but you get the idea): App.xaml, App.xaml.vb, MainPage.xaml, MainPage.xaml.vb, ChildWindow1.xaml, ChildWindow1.xaml.vb, ChildWindow2.xaml, ChildWindow2.xaml.vb, SilverlightApplication4.vbproj.
#realJSOP 22-Mar-11 15:10pm    
Email me, and I'll send you both projects in a zip file (we don't want univoters 1-bombing me for posting too much code).
AspDotNetDev 22-Mar-11 15:35pm    
I don't much like sharing my email (last I checked, Code Project shares my email when I "email reply" to a message). Instead, you can post the code in a reply to my message here:

Another problem I was having was that my app was becoming completely disabled (couldn't navigate or click on anything) when I exited the first child window more than once in a session. The fix was to add this code to my window_Closing event handler:

Application.Current.RootVisual.SetValue(Control.IsEnabledProperty, True)

It seems there is an as yet to be determined problem with the release version of SL4 when you're working in an app that was started in ANY earlier version of Silverlight. There is at least one conversation on the MSDN Silverlight forum regarding this bug.
Share this answer
Graeme_Grant 3-Apr-12 1:00am    
Hey John, It appears that this is not a true bug in the sense...

I've been having an odd timing issue with the ChildWindow where closing it before the opening animation completed, the Window closed fine however the app controls were not being enabled.

This brute force method resolved the issue for me. Thanks!
josephSurgeon 19-Oct-12 13:32pm    
thanks this helped me
The problem was happening because of this code:

Private Sub Button1_Click(ByVal sender As System.Object , ByVal e As System.Windows.RoutedEventArgs)
    Dim window As ChildWindow1 = New ChildWindow1()
    AddHandler window.Closed, AddressOf window1_Closed ' <--------------
End Sub

Private Sub window1_Closed(ByVal sender As System.Object , ByVal e As RoutedEventArgs) '<-------------
End Sub

It seems that RoutedEventArgs is NOT the correct argument type to use, and when the app tried to handle the Closed event, it would freak out as a result. Here's what C# gave me automatically (courtesy of intellisense):

private void Window1_Click(object sender, RoutedEventArgs e)
    ChildWindow1 window = new ChildWindow1();
    window.Closed += new EventHandler(window_Closed);  // <--------------

void window_Closed(object sender, EventArgs e)  // <---------------

Now, I'll freely admit that C# has gotten me into the habit of not paying attention to the event handler prototypes, but if this is going to throw an exception like this, it seems to me that the VB compiler should generate an error as a result of the invalid type. I can also blame VB because there is no intellisense adding the event handler method when you type AddHandler. C# just knows what to do and what you need.

In any event, the problem was that I was using RoutedEventArgs instead of EventArgs.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900