Export (0) Print
Expand All

Window Constructor

Initializes a new instance of the Window class.

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

'Declaration
Public Sub New

ExceptionCondition
InvalidOperationException

The application is not running outside the browser.

UnauthorizedAccessException

Application.HasElevatedPermissions is false.

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.

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


Silverlight

Supported in: 5, 4

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

Community Additions

ADD
Show:
© 2014 Microsoft