[已解决] Silverlight子窗口问题 [英] [SOLVED] Silverlight Child Window Problems

查看:103
本文介绍了[已解决] Silverlight子窗口问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[解决方案]请参阅我对这个问题的回答。



我的Silverlight 4应用程序中有两个子窗口问题。我在Silverlight应用程序中创建了一对ChildWindows。第一个子窗口有一个按钮,可以显示第二个子窗口。



问题#1



当我运行应用程序时,让第一个窗口出现,然后单击第一个子窗口中的按钮以显示第二个窗口。如果我在第二个窗口中单击*或* OK或取消按钮,我会收到以下异常:



无法转换类型的对象'System.EventArgs'键入'System.Windows.RoutedEventArgs'。



此异常发生在唯一的代码行中这两种方法只能将DialogResult设置为true或false。我在表单中添加了NO OTHER CODE(在VS设计器添加的代码之上)。



Visual Studio在创建窗口时自动添加以下代码。



 私人  Sub  OKButton_Click( ByVal  sender  As   Object  ByVal  e  As  RoutedEventArgs)句柄 OKButton.Click 
Me .DialogResult = True
结束 Sub

私有 < span class =code-keyword> Sub OKCancel_Click( ByVal sender As 对象 ByVal e As RoutedEventArgs)句柄 OKButton。点击
.DialogResult = 错误
结束 Sub





我也尝试删除这些方法,并明确地将一个Click属性添加到XAML,我的方法略有不同,但同样的例外:



< pre lang =cs> 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





问题#2



当我调出第一个子窗口,然后将其关闭时(使用OK按钮),它关闭就好了。如果我第二次这样做,它会很好地关闭,但整个应用程序似乎被禁用。无论我做什么,我都无法重新激活它。在试图确定为什么会发生这种情况时,我发现已关闭事件被调用两次。



To看看同一窗口是否发送了两次事件,我为窗口创建了一个GUID(在构造函数中),并在调试器中看到它确实发送了两次关闭事件,但它只处理了OK按钮单击(在表格即将结束)一次。



编辑#1 ==============



我创建了一个小测试应用程序(使用C#而不是VB)创建/显示第一个窗口,其中包含一个显示第二个窗口的按钮,它工作正常。对于问题#2,我认为这是我做过的事情,但问题#1让我感到困惑。



编辑#2 ======== ======



我使用VB创建完全相同的简单应用程序,问题#1存在。这是一个VB.Net的东西。



编辑#3 - 03/23 ==============



这是堆栈跟踪:



在Silverlight4App3.MainPage._Lambda $ __ 2(对象a0,EventArgs a1)System.Windows.Controls.ChildWindow.OnClosed(EventArgs e)
在System.Windows.Controls.ChildWindow.set_DialogResult的System.Windows.Controls.ChildWindow.Close()

(Nullable`1值)
在Silverlight4App3.ChildWindow1.OKButton_Click(Object sender,RoutedEventArgs e)
在System.Windows上的System.Windows.Controls.Primitives.ButtonBase.OnClick()
。 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)





一个人在Silverlight MSDN froums上回答了这个问题,建议我确保设计师使用的模板没有损坏,我的文件完全匹配。

解决方案

__2(对象a0,EventArgs a1)
在System.Windows.Controls.ChildWindow.OnClosed(EventArgs e)
在System.Windows.Controls.ChildWindow.Close()
在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()
在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 )





一个在Silverlight MSDN上回答这个问题的人,建议我确保设计师使用的模板不是已损坏,我的文件完全匹配。


我遇到的另一个问题是,当我退出第一个子窗口时,我的应用程序变得完全禁用(无法导航或点击任何内容)在一次会议中不止一次。修复是将此代码添加到我的 window_Closing 事件处理程序:



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





当您在任何早期版本的Silverlight中启动的应用程序中工作时,SL4的发布版本似乎还有待确定的问题。在MSDN Silverlight论坛上至少有一个关于此错误的对话。


问题出现了,因为这段代码:



 私有  Sub  Button1_Click( ByVal 发​​件人作为系统。对象 ByVal  e  As  System.Windows.RoutedEventArgs)
Dim window As ChildWindow1 = ChildWindow1()
AddHandler window.Closed, AddressOf window1_Closed ' < - ------------
window.Show()
结束 Sub

私有 Sub window1_Closed( ByVal sender As System。 Object ByVal e As RoutedEventArgs)' < -------------
结束 Sub





似乎 RoutedEventArgs 不是要使用的正确参数类型,当应用程序尝试处理已关闭事件时,它会因此而变得异常。这是C#自动给我的东西(由intellisense提供):



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

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





现在,我会自由地承认C#已经 得到了 我养成了不注意事件处理程序原型的习惯,但是如果要抛出这样的异常,我觉得VB编译器应该生成错误由于无效类型。我也可以责怪VB,因为当你输入 AddHandler 时,没有intellisense添加事件处理程序方法。 C#只知道该做什么以及你需要什么。



无论如何,问题在于我使用的是 RoutedEventArgs 而不是 EventArgs


[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.

解决方案

__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.


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.


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 ' <--------------
    window.Show()
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);  // <--------------
    window.Show();
}

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.


这篇关于[已解决] Silverlight子窗口问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆