2013-10-18 40 views
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 

回答

0

您可以綁定onBeforeUnload/BeforeUnload事件來詢問用戶。

+0

好的,我如何從Silverlight訪問它們? – OneFineDay

+0

HtmlPage.Window.AttachEvent(「beforeunload」,HandleBeforeUnload);並聲明public void HandleBeforeUnload(object sender,HtmlEventArgs args){...} –

+0

'beforeunload'事件應該匹配'ASPX'默認頁面中的事件嗎?我沒有看到有這個名字的人。 – OneFineDay

相關問題