0

我是用 VB.Net 用 Silverlight 5 编写的。我有许多子窗口。我想在用户关闭此窗口而不单击任何按钮时捕获事件。如果可能的话,请给我一个例子。我更喜欢VB,但我可以翻译C#。

鲍勃

4

1 回答 1

0

您可以订阅 ChildWindows 的 Closing 或 Closed 事件,在 Closing 事件中您可以检查 DialogResult 以查看它是否为 True,如果是则关闭窗口,否则通过将 e.Cancel 设置为 True 来取消 ChildWindow 的关闭。我在这里的示例显示了这两个事件,并允许您停止 ChildWindows 关闭。

Partial Public Class MainPage
    Inherits UserControl
    Dim child As ChildWindow1

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
        child = New ChildWindow1
        AddHandler child.Closed, AddressOf ChildClosed
        AddHandler child.Closing, AddressOf ChildClosing
        child.Show()
    End Sub

    Private Sub ChildClosed(sender As Object, e As EventArgs)
        Dim result As Boolean? = CType(sender, ChildWindow).DialogResult

        If IsNothing(result) Then
            'Do something if DialogResult is Nothing You can not cancel close with this event
        ElseIf Not result Then
            'Do what you want when the Cancel Button is clicked
        ElseIf result Then
            'Do what you want when the Ok Button is clicked
        End If
    End Sub

    Private Sub ChildClosing(sender As Object, e As ComponentModel.CancelEventArgs)
        Dim result As Boolean? = CType(sender, ChildWindow).DialogResult

        If IsNothing(result) Then
            e.Cancel = True 'This will cancel the ChildWindows Close and leave it open
        ElseIf Not result Then
            'Do what you want when the Cancel Button is clicked
        ElseIf result Then
            'Do what you want when the Ok Button is clicked
        End If
    End Sub

End Class
于 2012-11-03T00:45:11.160 回答