Sdílet prostřednictvím


Window Class

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

Represents an out-of-browser application window.

Inheritance Hierarchy

System.Object
  System.Windows.DependencyObject
    System.Windows.Window

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

Syntax

'Declaration
Public Class Window _
    Inherits DependencyObject
public class Window : DependencyObject

The Window type exposes the following members.

Constructors

  Name Description
Public method Window Initializes a new instance of the Window class.

Top

Properties

  Name Description
Public property Content Gets or sets the root visual element that represents the contents of the window.
Public property Dispatcher Gets the Dispatcher this object is associated with. (Inherited from DependencyObject.)
Public property Height Gets or sets the height of the application window in pixels.
Public property IsActive Gets a value that indicates whether the window is active.
Public property IsVisible Gets a value that indicates whether the window is currently visible in the user interface.
Public property Left Gets or sets the position of the left edge of the application window; see Remarks for restrictions on setting this property at run time.
Public property Title Gets or sets the window title bar text.
Public property Top Gets or sets the position of the top edge of the application window; see Remarks for restrictions on setting this property at run time.
Public property TopMost Gets or sets a value that indicates whether the application window is always displayed in front of other windows.
Public property Visibility Gets or sets a value that indicates whether the window is currently visible or collapsed.
Public property Width Gets or sets the width of the application window in pixels.
Public property WindowState Gets or sets a value that indicates whether the window is maximized, minimized, or in the normal state.
Public property WindowStyle Gets or sets a value that indicates the appearance of the title bar and border of the Window.

Top

Methods

  Name Description
Public method Activate Attempts to activate the application window by bringing it to the foreground and setting the input focus to it.
Public method CheckAccess Determines whether the calling thread has access to this object. (Inherited from DependencyObject.)
Public method ClearValue Clears the local value of a dependency property. (Inherited from DependencyObject.)
Public method Close Closes the application window unless the Closing event is canceled.
Public method DragMove In trusted applications, begins a mouse drag operation to move the window.
Public method DragResize In trusted applications, begins a mouse drag operation to move the specified window edge or corner.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public method GetAnimationBaseValue Returns any base value established for a Silverlight dependency property, which would apply in cases where an animation is not active. (Inherited from DependencyObject.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method GetValue Returns the current effective value of a dependency property from a DependencyObject. (Inherited from DependencyObject.)
Public methodStatic member GetWindow Gets the window that contains the specified DependencyObject.
Public method Hide Changes the value of the Visibility property of the Window to Collapsed.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ReadLocalValue Returns the local value of a dependency property, if a local value is set. (Inherited from DependencyObject.)
Public method SetValue Sets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject.)
Public method Show Changes the value of the Visibility property of the Window to Visible.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Events

  Name Description
Public event Closing Occurs when the window is about to close.

Top

Remarks

You can use this class to programmatically manipulate an out-of-browser application window at run time. However, in Silverlight 4, an application can have only one window, which is created automatically. To retrieve this Window instance, use the Application.MainWindow property. You can use this instance only if the application is currently running outside the browser. The properties of this instance are initialized from the corresponding WindowSettings properties in the application manifest.

You can set the properties of this class only in response to user-initiated actions unless the current application is a trusted application. For example, you can change the window size in a button Click event handler.

In trusted applications, you can remove the window title bar and border, and optionally, use rounded corners. For more information, see How to: Configure an Application for Out-of-Browser Support.

Without a title bar and border, however, there are no Minimize, Maximize, and Close buttons; and users cannot drag the window or its edges. You can replace the missing functionality by using the WindowState property and the Close, DragMove, and DragResize methods.

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.

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 Application.MainWindow value differs from additional Window instances in the following ways:

  • The main window is created automatically and configured using settings in the application manifest.

  • The main window controls the lifetime of all other windows. When the main window closes, all additional windows close automatically.

  • The Visibility property has a default value of Visible for the main window and Collapsed for all other windows.

  • The TopMost property is meaningful only for the main window.

  • Only the main window can have a WindowStyle property value of BorderlessRoundCornersWindow.

Examples

The following Silverlight 5 code examples show how a trusted, out-of-browser application can display an arbitrary user control in a separate window. These examples require a UserControl subclass named DemoUserControl.

The following code example shows how to create a window that will be used one time. Each time you create a new window with this code, it is added to the Application.Windows collection and given a unique Title based on the number of windows currently in the collection.

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
    };
}

By default, you cannot reuse a window after it is closed. To display and hide a single window multiple times, you must set its Visibility property and cancel its Closing event, as shown in the following code example.

Private Sub ShowHideDemoWindowButton_Click(
    sender As System.Object, e As System.Windows.RoutedEventArgs)

    If (Not Application.Current.IsRunningOutOfBrowser OrElse _ 
        Not Application.Current.HasElevatedPermissions) Then Return

    DemoWindow.Visibility = If(DemoWindow.IsVisible,
        Visibility.Collapsed, Visibility.Visible)

End Sub

Private _demoWindow As Window
Private ReadOnly Property DemoWindow As Window
    Get
        If (Not Application.Current.IsRunningOutOfBrowser OrElse
            Not Application.Current.HasElevatedPermissions) Then Return Nothing

        If _demoWindow Is Nothing Then
            _demoWindow = New Window With
            {
                .Title = "Demo Window",
                .Content = New DemoUserControl(),
                .Height = 300, .Width = 300, .Top = 0, .Left = 0
            }

            AddHandler _demoWindow.Closing,
                Sub(sender As Object,
                    e As System.ComponentModel.ClosingEventArgs)
                    If (e.IsCancelable) Then
                        e.Cancel = True
                        _demoWindow.Visibility = Visibility.Collapsed
                    End If
                End Sub
        End If

        Return _demoWindow
    End Get
End Property
private void ShowHideDemoWindowButton_Click(object sender, RoutedEventArgs e)
{
    if (!Application.Current.IsRunningOutOfBrowser || 
        !Application.Current.HasElevatedPermissions) return;
    DemoWindow.Visibility = DemoWindow.IsVisible ?
        Visibility.Collapsed : Visibility.Visible;
}

private Window _demoWindow;
private Window DemoWindow
{
    get
    {
        if (!Application.Current.IsRunningOutOfBrowser || 
            !Application.Current.HasElevatedPermissions) return null;

        if (_demoWindow == null)
        {
            _demoWindow = new Window()
            {
                Title = "Demo Window",
                Content = new DemoUserControl(),
                Height = 300, Width = 300, Top = 0, Left = 0
            };

            _demoWindow.Closing += (sender, e) =>
            {
                if (e.IsCancelable)
                {
                    e.Cancel = true;
                    _demoWindow.Visibility = Visibility.Collapsed;
                }
            };
        }

        return _demoWindow;
    }
}

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.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.