App Lifecycle And Windowing

Windows App SDK desktop apps behave like desktop apps, not UWP apps. They launch, create windows, receive activation arguments, and keep running until closed or exited.

Startup Shape

public partial class App : Application
{
    public static Window? MainWindow { get; private set; }

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

Use Application.OnLaunched for basic startup. For notifications or protocol/file activation, route activation arguments explicitly and decide whether to show the window.

Windowing Model

NeedAPI
XAML content windowMicrosoft.UI.Xaml.Window
Native top-level window managementMicrosoft.UI.Windowing.AppWindow
Interop with Win32/WPF/WinFormsHWND -> WindowId -> AppWindow
Presenter modesAppWindowPresenter and presenter-specific APIs
Title bar customizationAppWindow.TitleBar, Window.ExtendsContentIntoTitleBar, or current TitleBar control guidance

AppWindow From A WinUI Window

using Microsoft.UI;
using Microsoft.UI.Windowing;
using WinRT.Interop;

var hwnd = WindowNative.GetWindowHandle(App.MainWindow);
var windowId = Win32Interop.GetWindowIdFromWindow(hwnd);
var appWindow = AppWindow.GetFromWindowId(windowId);

appWindow.Resize(new Windows.Graphics.SizeInt32(1100, 760));

What Not To Use

  • Window.Current.
  • Window.Dispatcher.
  • CoreWindow.
  • ApplicationView.GetForCurrentView().
  • CoreApplicationViewTitleBar.

Those are UWP-era patterns and are wrong or unsupported in WinUI 3 desktop apps.

Gotcha: Track windows explicitly. Multi-window apps need a registry of open Window/AppWindow instances, not a global assumption that Window.Current exists.