Freigeben über


Window.Close Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Closes the application window unless the Closing event is canceled.

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public Sub Close
public void Close()

Exceptions

Exception Condition
NotSupportedException

The application is not running outside the browser.

SecurityException

When calling this method, except in response to a user-initiated action, the Application.Startup event has already occurred and Application.HasElevatedPermissions is false.

Remarks

You use this method to provide an alternative to the window Close button. This is particularly useful in trusted applications when you hide the window title bar and border. For information, see How to: Configure an Application for Out-of-Browser Support.

You can call this method only in the following cases:

In Silverlight 5, trusted, out-of-browser applications can create multiple Window instances and manipulate them programmatically. When a window is closed either by the user or by the Close method, the instance is removed from the Windows collection and the instance becomes invalid. If you want to reuse a Window instance, handle the Closing event, set Cancel to true, and then hide the window by setting its Visibility property to Collapsed.

Examples

The following code example demonstrates the use of this method.

' Close the main window.
Private Sub CloseButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    If Application.Current.IsRunningOutOfBrowser Then
        Application.Current.MainWindow.Close()
    End If
End Sub
// Close the main window.
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
    if (Application.Current.IsRunningOutOfBrowser)
    {
        Application.Current.MainWindow.Close();
    }
}

Version Information

Silverlight

Supported in: 5, 4

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.