Window Class

Definition

Represents the 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
Window
Attributes

Examples

Note

Building UWP apps is currently only supported in Experimental versions of WinUI 3.

OnLaunched

The following code example shows the OnLaunched method override generated for the WinUI in UWP (Experimental) template in Microsoft Visual Studio. This code demonstrates typical usage patterns for the Current and Content properties and the Activate method.

protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs e)
{
    Frame rootFrame = Window.Current.Content as Frame;

    // Do not repeat app initialization when the Window already has content,
    // just ensure that the window is active.
    if (rootFrame == null)
    {
        // Create a Frame to act as the navigation context and navigate to the first page.
        rootFrame = new Frame();

        rootFrame.NavigationFailed += OnNavigationFailed;

        if (e.UWPLaunchActivatedEventArgs.PreviousExecutionState == ApplicationExecutionState.Terminated)
        {
            //TODO: Load state from previously suspended application.
        }

        // Place the frame in the current Window.
        Window.Current.Content = rootFrame;
    }

    if (e.UWPLaunchActivatedEventArgs.PrelaunchActivated == false)
    {
        if (rootFrame.Content == null)
        {
            // When the navigation stack isn't restored navigate to the first page,
            // configuring the new page by passing required information as a navigation
            // parameter.
            rootFrame.Navigate(typeof(MainPage), e.Arguments);
        }
        // Ensure the current window is active.
        Window.Current.Activate();
    }
}

For comparison, here's the same override generated for the WinUI in Desktop template showing the desktop Window object.

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 a Desktop app you can create each Window explicitly. Creating more than one Window requires the 1.0.1 update to WindowsAppSDK, and is limited to a single thread.

Window 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

This object can be used in both Desktop and UWP applications.

WinUI 3 in the Windows App SDK experimental release channel: For desktop apps, you can create more than one Window per thread. For UWP apps, you can create only a single Window per thread. See Create a new Window in Examples.

In a UWP app, each UI thread already has a Window that can be retrieved using the static Window.Current property. Additional windows can be created with CoreApplicationView objects, which are always created on a new thread, and which automatically create an ApplicationView, CoreWindow, and Window for the new thread.

For Desktop apps, Window implements IWindowNative to enable interop through the Window's HWND (WindowHandle).

For UWP apps, both the static Application property and the static Current property return the Window object. From this object, an app can use the Dispatcher object or determine the size of the Window from the Bounds property.

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.

Constructors

Window()

Initializes a new instance of the Window class.

Properties

Bounds

Gets a Rect value containing 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

For UWP apps, gets the CoreWindow associated with this Window. Desktop apps always return null.

Current

For UWP apps, gets the window of the current thread. Desktop apps always return null.

Dispatcher

Gets the CoreDispatcher object for the Window when called from a UWP app. Returns null when called from a Desktop app.

Note

Window.Dispatcher may be altered or unavailable in future releases. Use Window.DispatcherQueue instead.

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.

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 the application window.

SetTitleBar(UIElement)

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

Events

Activated

Occurs when the window has successfully been activated.

Closed

Occurs when the window has closed.

SizeChanged

Occurs when the app window has first rendered or has changed its rendering size.

VisibilityChanged

Occurs when the value of the Visible property changes.

Applies to

See also