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)


Public Sub Close
public void Close()


Exception Condition

The application is not running outside the browser.


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


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.


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
    End If
End Sub
// Close the main window.
private void CloseButton_Click(object sender, RoutedEventArgs e)
    if (Application.Current.IsRunningOutOfBrowser)

Version Information


Supported in: 5, 4


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