私は VB.Net を使用して Silverlight 5 で作成しています。私はいくつかの子ウィンドウを持っています。ユーザーがボタンをクリックせずにこのウィンドウを閉じたときに、イベントをキャッチしたいと考えています。できれば例を教えてください。私は VB を好みますが、C# を翻訳できます。
ボブ
私は VB.Net を使用して Silverlight 5 で作成しています。私はいくつかの子ウィンドウを持っています。ユーザーがボタンをクリックせずにこのウィンドウを閉じたときに、イベントをキャッチしたいと考えています。できれば例を教えてください。私は VB を好みますが、C# を翻訳できます。
ボブ
ChildWindows の Closing イベントまたは Closed イベントをサブスクライブできます。Closing イベントでは、DialogResult をチェックして True かどうかを確認できます。そうでない場合は、ウィンドウを閉じます。それ以外の場合は、e.Cancel を True に設定して ChildWindow の終了をキャンセルします。ここにある例は両方のイベントを示しており、ChildWindows Close を停止できます。
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