Condividi tramite


Window Constructor

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

Initializes a new instance of the Window class.

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

Syntax

'Declaration
Public Sub New
public Window()

Exceptions

Exception Condition
InvalidOperationException

The application is not running outside the browser.

UnauthorizedAccessException

Application.HasElevatedPermissions is false.

Remarks

In Silverlight 4, you cannot use an instance of this class that you create yourself. To retrieve the main Window instance, use the Application.MainWindow property. You can use the value of this property only if the application is currently running outside the browser.

In Silverlight 5, trusted, out-of-browser applications can create multiple Window instances and manipulate them programmatically. This is useful to support flexible, multi-monitor layouts and to create features such as non-modal dialog boxes and tear-off windows.

To create and display a new window, you must programmatically call the Window() constructor, configure the instance, and then set its Visibility property to Visible, as shown in the Example section below. At a minimum, you must configure the instance by setting its Height, Width, and Content properties.

You cannot create a Window instance in XAML, nor create a Window subclass. However, you will typically set the Content property to a new instance of a user control that you define in XAML, as shown in the example.

On creation, a Window instance is automatically added to the Application.Windows collection, where you can access it later. This includes the Application.MainWindow instance, which is always the first item in the collection.

Examples

The following Silverlight 5 code example shows how a trusted, out-of-browser application can display an arbitrary user control in a separate window. This example requires a UserControl subclass named DemoUserControl.

If (Application.Current.IsRunningOutOfBrowser AndAlso _ 
    Application.Current.HasElevatedPermissions)

    Dim newWindow As New Window With
    {
        .Title = "Demo Window # " &
            Application.Current.Windows.Count.ToString(),
        .Height = 300,
        .Width = 300,
        .Top = 0,
        .Left = 0,
        .Content = New DemoUserControl(),
        .Visibility = Visibility.Visible
    }
End If
if (Application.Current.IsRunningOutOfBrowser && 
    Application.Current.HasElevatedPermissions)
{
    var newWindow = new Window()
    {
        Title = "Demo Window # " + 
            Application.Current.Windows.Count.ToString(),
        Height = 300, 
        Width = 300, 
        Top = 0, 
        Left = 0,
        Content = new DemoUserControl(),
        Visibility = Visibility.Visible
    };
}

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.

See Also

Reference

Other Resources