wpf-to-winui3-migration
Guide for migrating PowerToys modules from WPF to WinUI 3 (Windows App SDK). Use when asked to migrate WPF code, convert WPF XAML to WinUI, replace System.Windows namespaces with Microsoft.UI.Xaml, update Dispatcher to DispatcherQueue, replace DynamicResource with ThemeResource, migrate imaging APIs from System.Windows.Media.Imaging to Windows.Graphics.Imaging, convert WPF Window to WinUI Window, migrate .resx to .resw resources, migrate custom Observable/RelayCommand to CommunityToolkit.Mvvm source generators, handle WPF-UI (Lepo) to WinUI native control migration, or fix installer/build pipeline issues after migration. Keywords: WPF, WinUI, WinUI3, migration, porting, convert, namespace, XAML, Dispatcher, DispatcherQueue, imaging, BitmapImage, Window, ContentDialog, ThemeResource, DynamicResource, ResourceLoader, resw, resx, CommunityToolkit, ObservableProperty, WPF-UI, SizeToContent, AppWindow, SoftwareBitmap.
SKILL.md 原文预览
WPF to WinUI 3 Migration Skill
Migrate PowerToys modules from WPF (System.Windows.*) to WinUI 3 (Microsoft.UI.Xaml.* / Windows App SDK). Based on patterns validated in the ImageResizer module migration.
When to Use This Skill
- Migrate a PowerToys module from WPF to WinUI 3
- Convert WPF XAML files to WinUI 3 XAML
- Replace
System.Windowsnamespaces withMicrosoft.UI.Xaml - Migrate
Dispatcherusage toDispatcherQueue - Migrate custom
Observable/RelayCommandto CommunityToolkit.Mvvm source generators - Replace WPF-UI (Lepo) controls with native WinUI 3 controls
- Convert imaging code from
System.Windows.Media.ImagingtoWindows.Graphics.Imaging - Handle WPF
Windowvs WinUIWindowdifferences (sizing, positioning, SizeToContent) - Migrate resource files from
.resxto.reswwithResourceLoader - Fix installer/build pipeline issues after WinUI 3 migration
- Update project files, NuGet packages, and signing config
Prerequisites
- Visual Studio 2022 17.4+
- Windows App SDK NuGet package (
Microsoft.WindowsAppSDK) - .NET 8+ with
net8.0-windows10.0.19041.0TFM - Windows 10 1803+ (April 2018 Update or newer)
Migration Strategy
Phase-by-Phase Scope
Work on bounded problems, not the entire codebase at once. Each phase should compile before moving to the next.
- Project file — Update TFM, NuGet packages, set
<UseWinUI>true</UseWinUI> - Data models and business logic — No UI dependencies, migrate first
- MVVM framework — Replace custom Observable/RelayCommand with CommunityToolkit.Mvvm
- Resource strings — Migrate
.resx→.resw, introduceResourceLoaderInstance - Services and utilities — Replace
System.Windowstypes, async-ify imaging code - ViewModels — Update Dispatcher usage, binding patterns
- Views/Pages — Starting from leaf pages with fewest dependencies
- Main page / shell — Last, since it depends on everything
- App.xaml / startup code — Merge carefully (do NOT overwrite WinUI 3 boilerplate)
- Installer & build pipeline — Update WiX, signing, build events
- Tests — Adapt for WinUI 3 runtime, async patterns
Migration Contract: Prohibited Patterns
These rules capture human judgment and must be applied consistently across every file. Do NOT deviate.
Architecture prohibitions:
- Do NOT overwrite
App.xaml/App.xaml.cs— WinUI 3 has different lifecycle boilerplate. Merge resources and init code into the generated WinUI 3 App class. - Do NOT create Exe→WinExe
ProjectReference— Extract shared code to a Library project. Causes phantom build artifacts. - Do NOT instantiate services directly — Use DI and CommunityToolkit.Mvvm patterns.
- Do NOT create a
Windowsubclass for every dialog or sub-page — useContentDialogfor in-app dialogs andFrame/Pagenavigation for sub-views. SeparateWindowclasses are reserved for distinct top-level surfaces (e.g., FancyZones editor, OOBE). - Do NOT omit
WindowsPackageType=NoneandWindowsAppSDKSelfContained=true— Both are mandatory in the csproj for every WinUI 3 module in PowerToys. Without them the app crashes at startup withCOMException: ClassFactory cannot supply requested classbecause the WinUI 3 runtime DLLs are not found. - Do NOT default to a bare
Window, and do NOT hand-write code-behind for windowing that XAML can express — For a top-level window in a WinUI 3 module, useWinUIEx.WindowExor an existing PowerToys base derived from it (for example,TransparentWindowfor transient overlays).WindowExrestores WPF-likeWindowmembers as XAML properties (MinWidth/MinHeight,Width/Height,IsResizable,IsMaximizable/IsMinimizable,IsTitleBarVisible,IsAlwaysOnTop,IsShownInSwitchers,WindowState,SystemBackdrop) plus helpers (CenterOnScreen(),PersistenceId), so windowing is declared in XAML instead of manualAppWindow/OverlappedPresentercode-behind. This is the established PowerToys convention across ImageResizer, PowerDisplay, Peek, AdvancedPaste, MeasureTool, ShortcutGuide, Settings, Hosts, FileLocksmith, QuickAccess, and other WinUI 3 modules. Only drop to rawAppWindow/presenter code for behaviorWindowExdoes not expose. See Threading and Window Management → WindowEx.
XAML prohibitions:
- Do NOT use
{DynamicResource}— Replace with{ThemeResource}(theme-reactive) or{StaticResource}. - Do NOT use
{Binding}inSetter.Value— Not supported in WinUI 3. Use{StaticResource}. - Do NOT use
{x:Static}— Replace with{x:Bind},x:Uid, or code-behind. - Do NOT use
{x:Type}— Not supported. Usex:DataTypefor DataTemplate, or code-behind. - Do NOT use
clr-namespace:— Replace withusing:in all xmlns declarations. - Do NOT use
Style.Triggers/DataTrigger/EventTrigger— Replace withVisualStateManager
评论
