Silverlight
アプリの状態によっては、ページが閉じないようにする方法はありますか? 目的: このアプリで作業している間、特定のタスクを終了するまで開いておきたい部分があり、ブラウザーは通常どおりに反応します。偶発的な閉鎖が起こらないようにしたいだけです。これが不可能な場合でも問題ありません。回避策を講じます。アプリはASPX
ページでホストされます。
いくつかの作業の後、ここにラッパー(VB> NET)があります。
Public Class SWC
''' <summary>Event arguments for handling the window close event.</summary>
Public Class HtmlWindowCloseEventArgs : Inherits CancelEventArgs
''' <summary>Gets or sets the message to display to the user asking them if they want to continue the window close.</summary>
Public Property DialogMessage() As String
Get
Return _dialogMessage
End Get
Set(value As String)
_dialogMessage = value
End Set
End Property
Private _dialogMessage As String
End Class
''' <summary>Monitors the closing of the HTML window.</summary>
Public Class HtmlWindowCloseMonitor
#Region "Events"
''' <summary>Fires when immediately before the window closes.</summary>
Public Shared Event WindowClosing As EventHandler(Of HtmlWindowCloseEventArgs)
Private Shared Sub OnWindowClosing(sender As Object, e As HtmlWindowCloseEventArgs)
RaiseEvent WindowClosing(sender, e)
End Sub
#End Region
#Region "Head"
Private Const ScriptableObjectName As String = "HtmlWindowCloseMonitor"
Private Const DefaultDialogMessage As String = "Are you sure you want to close the application?"
Private Shared ReadOnly instance As HtmlWindowCloseMonitor
''' <summary>Constructor.</summary>
Shared Sub New()
If instance Is Nothing Then
instance = New HtmlWindowCloseMonitor()
End If
End Sub
Private Sub New()
' Register the scriptable callback member.
HtmlPage.RegisterScriptableObject(ScriptableObjectName, Me)
' Retrieve the name of the plugin.
Dim pluginName = HtmlPage.Plugin.Id
If pluginName Is Nothing Then
Throw New Exception("Cannot register the 'onbeforeunload' event because the Silverlight <object> does not have an ID. Add an ID attribute to the Silverlight <object> host tag.")
End If
' Wire up event.
Dim eventFunction = String.Format("window.onbeforeunload = function () {{" & Environment.NewLine &
"var slApp = document.getElementById('{0}');" & Environment.NewLine &
"var result = slApp.Content.{1}.OnBeforeUnload();" & Environment.NewLine &
"if(result != null && result.length > 0)" & Environment.NewLine &
"return result;" & Environment.NewLine & "}}", pluginName, ScriptableObjectName)
HtmlPage.Window.Eval(eventFunction)
End Sub
#End Region
#Region "Methods"
<ScriptableMember()> _
Public Function OnBeforeUnload() As String
' Check with event-listeners to see if any of them want to cancel the window-close operation.
Dim args = New HtmlWindowCloseEventArgs()
OnWindowClosing(Me, args)
If Not args.Cancel Then
' No one wanted to stop the window from closing.
Return Nothing
End If
' Present the 'Are you sure' dialog to the use (via the browser).
Dim message = If(args.DialogMessage IsNot Nothing, args.DialogMessage, DefaultDialogMessage)
Return message
End Function
#End Region
End Class
End Class