Share via


Window Class

Definition

Represents a window of the current Application.

/// [Microsoft.UI.Xaml.Markup.ContentProperty(Name="Content")]
/// [Windows.Foundation.Metadata.ContractVersion(Microsoft.UI.Xaml.WinUIContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class Window
[Microsoft.UI.Xaml.Markup.ContentProperty(Name="Content")]
[Windows.Foundation.Metadata.ContractVersion(typeof(Microsoft.UI.Xaml.WinUIContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public class Window
Public Class Window
Inheritance
Object IInspectable Window
Attributes

Examples

OnLaunched

The following code example shows the OnLaunched method override generated for the WinUI in Desktop template in Microsoft Visual Studio. This code demonstrates typical usage of the Activate method on Window.

protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
    m_window = new MainWindow();
    m_window.Activate();
}

private Window m_window;

Create a new Window

In your apps you can create each Window explicitly. Creating more than one Window requires the 1.0.1 update to Windows App SDK, and is limited to a single thread.

var window = new Window();
window.Content = new TextBlock() { Text = "Hello" };
window.Activate();

You can also define a new Window in markup:

<Window 
    x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <TextBlock>Hello</TextBlock>
</Window>

with corresponding code-behind:

public partial class MainWindow : Window
{
  public MainWindow()
  {
    InitializeComponent();
  }
}

...
MainWindow window = new MainWindow();
window.Activate();

Remarks

For more information and examples, see Windowing overview for WinUI and Windows App SDK.

Typically, Window is used to set a UIElement to the Content that represents the app UI. This is usually done as part of app activation (for example in the OnLaunched override) and allows you to change the window content throughout the lifetime of the window.

Ensure you call Activate on any Window used on initial activation. If you use the default app templates from Microsoft Visual Studio, Window.Activate is included in the App.xaml code-behind file.

You can create more than one Window per thread in your apps. See Show multiple windows for your app.

Window implements IWindowNative to enable interop through the Window's HWND (WindowHandle).

Constructors

Window()

Initializes a new instance of the Window class.

Properties

AppWindow

Gets the AppWindow associated with this XAML Window.

Bounds

Gets a Rect value that contains the height and width of the application window in units of effective (view) pixels.

Compositor

Gets the Compositor for this window.

Content

Gets or sets the visual root of an application window.

CoreWindow

[Deprecated] Always returns null.

Current

Always returns null.

Dispatcher

[Deprecated] Always returns null.

DispatcherQueue

Gets the DispatcherQueue object for the window.

ExtendsContentIntoTitleBar

Gets or sets a value that specifies whether the default title bar of the window should be hidden to create space for app content.

SystemBackdrop

Gets or sets the system backdrop to apply to this Window. The backdrop is rendered behind the Window content.

Title

Gets or sets a string used for the window title.

Visible

Gets a value that reports whether the window is visible.

Methods

Activate()

Attempts to activate the application window by bringing it to the foreground and setting the input focus to it.

Close()

Closes (destroys) the application window.

SetTitleBar(UIElement)

Enables title bar behavior on a XAML element when ExtendsContentIntoTitleBar is true.

Events

Activated

Occurs when the window has been successfully activated or deactivated.

Closed

Occurs when the window has closed.

SizeChanged

Occurs when the rendering size of the window has changed.

VisibilityChanged

Occurs when the value of the Visible property changes.

Applies to

See also