0

有没有办法根据我的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
4

1 回答 1

0

您可以绑定 onBeforeUnload / BeforeUnload 事件来询问用户。

于 2013-10-18T17:11:09.227 回答