winui3-migration-guide by github/awesome-copilot
npx skills add https://github.com/github/awesome-copilot --skill winui3-migration-guide在将 UWP 应用迁移到 WinUI 3 / Windows App SDK 时,或在验证生成的代码是否使用了正确的 WinUI 3 API 而非遗留的 UWP 模式时,请使用此技能。
所有 Windows.UI.Xaml.* 命名空间都迁移到 Microsoft.UI.Xaml.*:
| UWP 命名空间 | WinUI 3 命名空间 |
|---|---|
Windows.UI.Xaml | Microsoft.UI.Xaml |
Windows.UI.Xaml.Controls | Microsoft.UI.Xaml.Controls |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
Windows.UI.Xaml.Media | Microsoft.UI.Xaml.Media |
Windows.UI.Xaml.Input | Microsoft.UI.Xaml.Input |
Windows.UI.Xaml.Data | Microsoft.UI.Xaml.Data |
Windows.UI.Xaml.Navigation | Microsoft.UI.Xaml.Navigation |
Windows.UI.Xaml.Shapes | Microsoft.UI.Xaml.Shapes |
Windows.UI.Composition | Microsoft.UI.Composition |
Windows.UI.Input | Microsoft.UI.Input |
Windows.UI.Colors | Microsoft.UI.Colors |
Windows.UI.Text | Microsoft.UI.Text |
Windows.UI.Core | Microsoft.UI.Dispatching (用于调度程序) |
// ❌ 错误 — 在 WinUI 3 中会抛出 InvalidOperationException
var dialog = new ContentDialog
{
Title = "Error",
Content = "Something went wrong.",
CloseButtonText = "OK"
};
await dialog.ShowAsync();
// ✅ 正确 — 在显示前设置 XamlRoot
var dialog = new ContentDialog
{
Title = "Error",
Content = "Something went wrong.",
CloseButtonText = "OK",
XamlRoot = this.Content.XamlRoot // WinUI 3 中必需
};
await dialog.ShowAsync();
// ❌ 错误 — UWP API,在 WinUI 3 桌面版中不可用
var dialog = new Windows.UI.Popups.MessageDialog("Are you sure?", "Confirm");
await dialog.ShowAsync();
// ✅ 正确 — 使用 ContentDialog
var dialog = new ContentDialog
{
Title = "Confirm",
Content = "Are you sure?",
PrimaryButtonText = "Yes",
CloseButtonText = "No",
XamlRoot = this.Content.XamlRoot
};
var result = await dialog.ShowAsync();
if (result == ContentDialogResult.Primary)
{
// 用户确认
}
// ❌ 错误 — CoreDispatcher 在 WinUI 3 中不存在
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
StatusText.Text = "Done";
});
// ✅ 正确 — 使用 DispatcherQueue
DispatcherQueue.TryEnqueue(() =>
{
StatusText.Text = "Done";
});
// 带优先级:
DispatcherQueue.TryEnqueue(DispatcherQueuePriority.High, () =>
{
ProgressBar.Value = 100;
});
// ❌ 错误 — Window.Current 在 WinUI 3 中不存在
var currentWindow = Window.Current;
// ✅ 正确 — 在 App 中使用静态属性
public partial class App : Application
{
public static Window MainWindow { get; private set; }
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
MainWindow = new MainWindow();
MainWindow.Activate();
}
}
// 在任何地方访问:App.MainWindow
| UWP API | WinUI 3 API |
|---|---|
ApplicationView.TryResizeView() | AppWindow.Resize() |
AppWindow.TryCreateAsync() | AppWindow.Create() |
AppWindow.TryShowAsync() | AppWindow.Show() |
AppWindow.TryConsolidateAsync() | AppWindow.Destroy() |
AppWindow.RequestMoveXxx() | AppWindow.Move() |
AppWindow.GetPlacement() | AppWindow.Position 属性 |
AppWindow.RequestPresentation() | AppWindow.SetPresenter() |
| UWP API | WinUI 3 API |
|---|---|
CoreApplicationViewTitleBar | AppWindowTitleBar |
CoreApplicationView.TitleBar.ExtendViewIntoTitleBar | AppWindow.TitleBar.ExtendsContentIntoTitleBar |
// ❌ 错误 — UWP 风格,没有窗口句柄
var picker = new FileOpenPicker();
picker.FileTypeFilter.Add(".txt");
var file = await picker.PickSingleFileAsync();
// ✅ 正确 — 使用窗口句柄初始化
var picker = new FileOpenPicker();
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(App.MainWindow);
WinRT.Interop.InitializeWithWindow.Initialize(picker, hwnd);
picker.FileTypeFilter.Add(".txt");
var file = await picker.PickSingleFileAsync();
| UWP 模式 | WinUI 3 等效方案 |
|---|---|
CoreDispatcher.RunAsync(priority, callback) | DispatcherQueue.TryEnqueue(priority, callback) |
Dispatcher.HasThreadAccess | DispatcherQueue.HasThreadAccess |
CoreDispatcher.ProcessEvents() | 无等效方案 — 重构异步代码 |
CoreWindow.GetForCurrentThread() | 不可用 — 使用 DispatcherQueue.GetForCurrentThread() |
关键区别:UWP 使用 ASTA(应用程序单线程单元)并内置了重入阻塞。WinUI 3 使用标准 STA,没有这种保护。当异步代码泵送消息时,请注意重入问题。
// ❌ 错误 — UWP IBackgroundTask
public sealed class MyTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance) { }
}
// ✅ 正确 — Windows App SDK AppLifecycle
using Microsoft.Windows.AppLifecycle;
// 注册激活
var args = AppInstance.GetCurrent().GetActivatedEventArgs();
if (args.Kind == ExtendedActivationKind.AppNotification)
{
// 处理后台激活
}
| 场景 | 打包应用 | 非打包应用 |
|---|---|---|
| 简单设置 | ApplicationData.Current.LocalSettings | LocalApplicationData 中的 JSON 文件 |
| 本地文件存储 | ApplicationData.Current.LocalFolder | Environment.GetFolderPath(SpecialFolder.LocalApplicationData) |
所有 GetForCurrentView() 模式在 WinUI 3 桌面应用中均不可用:
| UWP API | WinUI 3 替换方案 |
|---|---|
UIViewSettings.GetForCurrentView() | 使用 AppWindow 属性 |
ApplicationView.GetForCurrentView() | AppWindow.GetFromWindowId(windowId) |
DisplayInformation.GetForCurrentView() | Win32 GetDpiForWindow() 或 XamlRoot.RasterizationScale |
CoreApplication.GetCurrentView() | 不可用 — 手动跟踪窗口 |
SystemNavigationManager.GetForCurrentView() | 直接在 NavigationView 中处理后向导航 |
UWP 单元测试项目不适用于 WinUI 3。您必须迁移到 WinUI 3 测试项目模板。
| UWP | WinUI 3 |
|---|---|
| 单元测试应用(通用 Windows) | 单元测试应用(桌面版 WinUI) |
| 包含 UWP 类型的标准 MSTest 项目 | 必须使用 WinUI 测试应用以支持 Xaml 运行时 |
所有测试使用 [TestMethod] | 逻辑测试使用 [TestMethod],XAML/UI 测试使用 [UITestMethod] |
| 类库(通用 Windows) | 类库(桌面版 WinUI) |
// ✅ WinUI 3 单元测试 — 任何 XAML 交互都使用 [UITestMethod]
[UITestMethod]
public void TestMyControl()
{
var control = new MyLibrary.MyUserControl();
Assert.AreEqual(expected, control.MyProperty);
}
关键点:[UITestMethod] 属性告诉测试运行程序在 XAML UI 线程上执行测试,这对于实例化任何 Microsoft.UI.Xaml 类型是必需的。
Windows.UI.Xaml.* 的 using 指令替换为 Microsoft.UI.Xaml.*Windows.UI.Colors 替换为 Microsoft.UI.ColorsCoreDispatcher.RunAsync 替换为 DispatcherQueue.TryEnqueueWindow.Current 替换为 App.MainWindow 静态属性ContentDialog 实例添加 XamlRootInitializeWithWindow.Initialize(picker, hwnd) 初始化所有选择器MessageDialog 替换为 ContentDialogApplicationView/CoreWindow 替换为 AppWindowCoreApplicationViewTitleBar 替换为 AppWindowTitleBarGetForCurrentView() 调用替换为等效的 AppWindow 方案IBackgroundTask 替换为 AppLifecycle 激活net10.0-windows10.0.22621.0,添加 <UseWinUI>true</UseWinUI>[UITestMethod]每周安装量
4.3K
代码库
GitHub 星标数
26.7K
首次出现
2026年3月3日
安全审计
安装于
codex4.2K
gemini-cli4.2K
opencode4.2K
cursor4.2K
github-copilot4.2K
kimi-cli4.2K
Use this skill when migrating UWP apps to WinUI 3 / Windows App SDK, or when verifying that generated code uses correct WinUI 3 APIs instead of legacy UWP patterns.
All Windows.UI.Xaml.* namespaces move to Microsoft.UI.Xaml.*:
| UWP Namespace | WinUI 3 Namespace |
|---|---|
Windows.UI.Xaml | Microsoft.UI.Xaml |
Windows.UI.Xaml.Controls | Microsoft.UI.Xaml.Controls |
Windows.UI.Xaml.Media | Microsoft.UI.Xaml.Media |
Windows.UI.Xaml.Input | Microsoft.UI.Xaml.Input |
Windows.UI.Xaml.Data | Microsoft.UI.Xaml.Data |
Windows.UI.Xaml.Navigation | Microsoft.UI.Xaml.Navigation |
Windows.UI.Xaml.Shapes | Microsoft.UI.Xaml.Shapes |
Windows.UI.Composition | Microsoft.UI.Composition |
Windows.UI.Input | Microsoft.UI.Input |
Windows.UI.Colors | Microsoft.UI.Colors |
Windows.UI.Text | Microsoft.UI.Text |
Windows.UI.Core | Microsoft.UI.Dispatching (for dispatcher) |
// ❌ WRONG — Throws InvalidOperationException in WinUI 3
var dialog = new ContentDialog
{
Title = "Error",
Content = "Something went wrong.",
CloseButtonText = "OK"
};
await dialog.ShowAsync();
// ✅ CORRECT — Set XamlRoot before showing
var dialog = new ContentDialog
{
Title = "Error",
Content = "Something went wrong.",
CloseButtonText = "OK",
XamlRoot = this.Content.XamlRoot // Required in WinUI 3
};
await dialog.ShowAsync();
// ❌ WRONG — UWP API, not available in WinUI 3 desktop
var dialog = new Windows.UI.Popups.MessageDialog("Are you sure?", "Confirm");
await dialog.ShowAsync();
// ✅ CORRECT — Use ContentDialog
var dialog = new ContentDialog
{
Title = "Confirm",
Content = "Are you sure?",
PrimaryButtonText = "Yes",
CloseButtonText = "No",
XamlRoot = this.Content.XamlRoot
};
var result = await dialog.ShowAsync();
if (result == ContentDialogResult.Primary)
{
// User confirmed
}
// ❌ WRONG — CoreDispatcher does not exist in WinUI 3
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
StatusText.Text = "Done";
});
// ✅ CORRECT — Use DispatcherQueue
DispatcherQueue.TryEnqueue(() =>
{
StatusText.Text = "Done";
});
// With priority:
DispatcherQueue.TryEnqueue(DispatcherQueuePriority.High, () =>
{
ProgressBar.Value = 100;
});
// ❌ WRONG — Window.Current does not exist in WinUI 3
var currentWindow = Window.Current;
// ✅ CORRECT — Use a static property in App
public partial class App : Application
{
public static Window MainWindow { get; private set; }
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
MainWindow = new MainWindow();
MainWindow.Activate();
}
}
// Access anywhere: App.MainWindow
| UWP API | WinUI 3 API |
|---|---|
ApplicationView.TryResizeView() | AppWindow.Resize() |
AppWindow.TryCreateAsync() | AppWindow.Create() |
AppWindow.TryShowAsync() | AppWindow.Show() |
AppWindow.TryConsolidateAsync() | AppWindow.Destroy() |
| UWP API | WinUI 3 API |
|---|---|
CoreApplicationViewTitleBar | AppWindowTitleBar |
CoreApplicationView.TitleBar.ExtendViewIntoTitleBar | AppWindow.TitleBar.ExtendsContentIntoTitleBar |
// ❌ WRONG — UWP style, no window handle
var picker = new FileOpenPicker();
picker.FileTypeFilter.Add(".txt");
var file = await picker.PickSingleFileAsync();
// ✅ CORRECT — Initialize with window handle
var picker = new FileOpenPicker();
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(App.MainWindow);
WinRT.Interop.InitializeWithWindow.Initialize(picker, hwnd);
picker.FileTypeFilter.Add(".txt");
var file = await picker.PickSingleFileAsync();
| UWP Pattern | WinUI 3 Equivalent |
|---|---|
CoreDispatcher.RunAsync(priority, callback) | DispatcherQueue.TryEnqueue(priority, callback) |
Dispatcher.HasThreadAccess | DispatcherQueue.HasThreadAccess |
CoreDispatcher.ProcessEvents() | No equivalent — restructure async code |
CoreWindow.GetForCurrentThread() | Not available — use DispatcherQueue.GetForCurrentThread() |
Key difference : UWP uses ASTA (Application STA) with built-in reentrancy blocking. WinUI 3 uses standard STA without this protection. Watch for reentrancy issues when async code pumps messages.
// ❌ WRONG — UWP IBackgroundTask
public sealed class MyTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance) { }
}
// ✅ CORRECT — Windows App SDK AppLifecycle
using Microsoft.Windows.AppLifecycle;
// Register for activation
var args = AppInstance.GetCurrent().GetActivatedEventArgs();
if (args.Kind == ExtendedActivationKind.AppNotification)
{
// Handle background activation
}
| Scenario | Packaged App | Unpackaged App |
|---|---|---|
| Simple settings | ApplicationData.Current.LocalSettings | JSON file in LocalApplicationData |
| Local file storage | ApplicationData.Current.LocalFolder | Environment.GetFolderPath(SpecialFolder.LocalApplicationData) |
All GetForCurrentView() patterns are unavailable in WinUI 3 desktop apps:
| UWP API | WinUI 3 Replacement |
|---|---|
UIViewSettings.GetForCurrentView() | Use AppWindow properties |
ApplicationView.GetForCurrentView() | AppWindow.GetFromWindowId(windowId) |
DisplayInformation.GetForCurrentView() | Win32 GetDpiForWindow() or XamlRoot.RasterizationScale |
UWP unit test projects do not work with WinUI 3. You must migrate to the WinUI 3 test project templates.
| UWP | WinUI 3 |
|---|---|
| Unit Test App (Universal Windows) | Unit Test App (WinUI in Desktop) |
| Standard MSTest project with UWP types | Must use WinUI test app for Xaml runtime |
[TestMethod] for all tests | [TestMethod] for logic, [UITestMethod] for XAML/UI tests |
| Class Library (Universal Windows) | Class Library (WinUI in Desktop) |
// ✅ WinUI 3 unit test — use [UITestMethod] for any XAML interaction
[UITestMethod]
public void TestMyControl()
{
var control = new MyLibrary.MyUserControl();
Assert.AreEqual(expected, control.MyProperty);
}
Key: The [UITestMethod] attribute tells the test runner to execute the test on the XAML UI thread, which is required for instantiating any Microsoft.UI.Xaml type.
Windows.UI.Xaml.* using directives with Microsoft.UI.Xaml.*Windows.UI.Colors with Microsoft.UI.ColorsCoreDispatcher.RunAsync with DispatcherQueue.TryEnqueueWindow.Current with App.MainWindow static propertyXamlRoot to all ContentDialog instancesWeekly Installs
4.3K
Repository
GitHub Stars
26.7K
First Seen
Mar 3, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex4.2K
gemini-cli4.2K
opencode4.2K
cursor4.2K
github-copilot4.2K
kimi-cli4.2K
99,500 周安装
AppWindow.RequestMoveXxx() | AppWindow.Move() |
AppWindow.GetPlacement() | AppWindow.Position property |
AppWindow.RequestPresentation() | AppWindow.SetPresenter() |
CoreApplication.GetCurrentView()| Not available — track windows manually |
SystemNavigationManager.GetForCurrentView() | Handle back navigation in NavigationView directly |
InitializeWithWindow.Initialize(picker, hwnd)MessageDialog with ContentDialogApplicationView/CoreWindow with AppWindowCoreApplicationViewTitleBar with AppWindowTitleBarGetForCurrentView() calls with AppWindow equivalentsIBackgroundTask with AppLifecycle activationnet10.0-windows10.0.22621.0, add <UseWinUI>true</UseWinUI>[UITestMethod] for XAML tests