WinUI 3 Migration Guide

WinUI 3 Migration Guide

Maps legacy UWP APIs to their WinUI 3 and Windows App SDK equivalents with before and after snippets, and flags the most common AI code generation mistakes.

Skill files1 file
SKILL.md278 lines
1
---
2
name: winui3-migration-guide
3
description: 'UWP-to-WinUI 3 migration reference. Maps legacy UWP APIs to correct Windows App SDK equivalents with before/after code snippets. Covers namespace changes, threading (CoreDispatcher to DispatcherQueue), windowing (CoreWindow to AppWindow), dialogs, pickers, sharing, printing, background tasks, and the most common Copilot code generation mistakes.'
4
---
5
6
# WinUI 3 Migration Guide
7
8
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.
9
10
---
11
12
## Namespace Changes
13
14
All `Windows.UI.Xaml.*` namespaces move to `Microsoft.UI.Xaml.*`:
15
16
| UWP Namespace | WinUI 3 Namespace |
17
|--------------|-------------------|
18
| `Windows.UI.Xaml` | `Microsoft.UI.Xaml` |
19
| `Windows.UI.Xaml.Controls` | `Microsoft.UI.Xaml.Controls` |
20
| `Windows.UI.Xaml.Media` | `Microsoft.UI.Xaml.Media` |
21
| `Windows.UI.Xaml.Input` | `Microsoft.UI.Xaml.Input` |
22
| `Windows.UI.Xaml.Data` | `Microsoft.UI.Xaml.Data` |
23
| `Windows.UI.Xaml.Navigation` | `Microsoft.UI.Xaml.Navigation` |
24
| `Windows.UI.Xaml.Shapes` | `Microsoft.UI.Xaml.Shapes` |
25
| `Windows.UI.Composition` | `Microsoft.UI.Composition` |
26
| `Windows.UI.Input` | `Microsoft.UI.Input` |
27
| `Windows.UI.Colors` | `Microsoft.UI.Colors` |
28
| `Windows.UI.Text` | `Microsoft.UI.Text` |
29
| `Windows.UI.Core` | `Microsoft.UI.Dispatching` (for dispatcher) |
30
31
---
32
33
## Top 3 Most Common Copilot Mistakes
34
35
### 1. ContentDialog Without XamlRoot
36
37
```csharp
38
// ❌ WRONG — Throws InvalidOperationException in WinUI 3
39
var dialog = new ContentDialog
40
{
41
Title = "Error",
42
Content = "Something went wrong.",
43
CloseButtonText = "OK"
44
};
45
await dialog.ShowAsync();
46
```
47
48
```csharp
49
// ✅ CORRECT — Set XamlRoot before showing
50
var dialog = new ContentDialog
51
{
52
Title = "Error",
53
Content = "Something went wrong.",
54
CloseButtonText = "OK",
55
XamlRoot = this.Content.XamlRoot // Required in WinUI 3
56
};
57
await dialog.ShowAsync();
58
```
59
60
### 2. MessageDialog Instead of ContentDialog
61
62
```csharp
63
// ❌ WRONG — UWP API, not available in WinUI 3 desktop
64
var dialog = new Windows.UI.Popups.MessageDialog("Are you sure?", "Confirm");
65
await dialog.ShowAsync();
66
```
67
68
```csharp
69
// ✅ CORRECT — Use ContentDialog
70
var dialog = new ContentDialog
71
{
72
Title = "Confirm",
73
Content = "Are you sure?",
74
PrimaryButtonText = "Yes",
75
CloseButtonText = "No",
76
XamlRoot = this.Content.XamlRoot
77
};
78
var result = await dialog.ShowAsync();
79
if (result == ContentDialogResult.Primary)
80
{
81
// User confirmed
82
}
83
```
84
85
### 3. CoreDispatcher Instead of DispatcherQueue
86
87
```csharp
88
// ❌ WRONG — CoreDispatcher does not exist in WinUI 3
89
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
90
{
91
StatusText.Text = "Done";
92
});
93
```
94
95
```csharp
96
// ✅ CORRECT — Use DispatcherQueue
97
DispatcherQueue.TryEnqueue(() =>
98
{
99
StatusText.Text = "Done";
100
});
101
102
// With priority:
103
DispatcherQueue.TryEnqueue(DispatcherQueuePriority.High, () =>
104
{
105
ProgressBar.Value = 100;
106
});
107
```
108
109
---
110
111
## Windowing Migration
112
113
### Window Reference
114
115
```csharp
116
// ❌ WRONG — Window.Current does not exist in WinUI 3
117
var currentWindow = Window.Current;
118
```
119
120
```csharp
121
// ✅ CORRECT — Use a static property in App
122
public partial class App : Application
123
{
124
public static Window MainWindow { get; private set; }
125
126
protected override void OnLaunched(LaunchActivatedEventArgs args)
127
{
128
MainWindow = new MainWindow();
129
MainWindow.Activate();
130
}
131
}
132
// Access anywhere: App.MainWindow
133
```
134
135
### Window Management
136
137
| UWP API | WinUI 3 API |
138
|---------|-------------|
139
| `ApplicationView.TryResizeView()` | `AppWindow.Resize()` |
140
| `AppWindow.TryCreateAsync()` | `AppWindow.Create()` |
141
| `AppWindow.TryShowAsync()` | `AppWindow.Show()` |
142
| `AppWindow.TryConsolidateAsync()` | `AppWindow.Destroy()` |
143
| `AppWindow.RequestMoveXxx()` | `AppWindow.Move()` |
144
| `AppWindow.GetPlacement()` | `AppWindow.Position` property |
145
| `AppWindow.RequestPresentation()` | `AppWindow.SetPresenter()` |
146
147
### Title Bar
148
149
| UWP API | WinUI 3 API |
150
|---------|-------------|
151
| `CoreApplicationViewTitleBar` | `AppWindowTitleBar` |
152
| `CoreApplicationView.TitleBar.ExtendViewIntoTitleBar` | `AppWindow.TitleBar.ExtendsContentIntoTitleBar` |
153
154
---
155
156
## Dialogs and Pickers Migration
157
158
### File/Folder Pickers
159
160
```csharp
161
// ❌ WRONG — UWP style, no window handle
162
var picker = new FileOpenPicker();
163
picker.FileTypeFilter.Add(".txt");
164
var file = await picker.PickSingleFileAsync();
165
```
166
167
```csharp
168
// ✅ CORRECT — Initialize with window handle
169
var picker = new FileOpenPicker();
170
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(App.MainWindow);
171
WinRT.Interop.InitializeWithWindow.Initialize(picker, hwnd);
172
picker.FileTypeFilter.Add(".txt");
173
var file = await picker.PickSingleFileAsync();
174
```
175
176
## Threading Migration
177
178
| UWP Pattern | WinUI 3 Equivalent |
179
|-------------|-------------------|
180
| `CoreDispatcher.RunAsync(priority, callback)` | `DispatcherQueue.TryEnqueue(priority, callback)` |
181
| `Dispatcher.HasThreadAccess` | `DispatcherQueue.HasThreadAccess` |
182
| `CoreDispatcher.ProcessEvents()` | No equivalent — restructure async code |
183
| `CoreWindow.GetForCurrentThread()` | Not available — use `DispatcherQueue.GetForCurrentThread()` |
184
185
**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.
186
187
---
188
189
## Background Tasks Migration
190
191
```csharp
192
// ❌ WRONG — UWP IBackgroundTask
193
public sealed class MyTask : IBackgroundTask
194
{
195
public void Run(IBackgroundTaskInstance taskInstance) { }
196
}
197
```
198
199
```csharp
200
// ✅ CORRECT — Windows App SDK AppLifecycle
201
using Microsoft.Windows.AppLifecycle;
202
203
// Register for activation
204
var args = AppInstance.GetCurrent().GetActivatedEventArgs();
205
if (args.Kind == ExtendedActivationKind.AppNotification)
206
{
207
// Handle background activation
208
}
209
```
210
211
---
212
213
## App Settings Migration
214
215
| Scenario | Packaged App | Unpackaged App |
216
|----------|-------------|----------------|
217
| Simple settings | `ApplicationData.Current.LocalSettings` | JSON file in `LocalApplicationData` |
218
| Local file storage | `ApplicationData.Current.LocalFolder` | `Environment.GetFolderPath(SpecialFolder.LocalApplicationData)` |
219
220
---
221
222
## GetForCurrentView() Replacements
223
224
All `GetForCurrentView()` patterns are unavailable in WinUI 3 desktop apps:
225
226
| UWP API | WinUI 3 Replacement |
227
|---------|-------------------|
228
| `UIViewSettings.GetForCurrentView()` | Use `AppWindow` properties |
229
| `ApplicationView.GetForCurrentView()` | `AppWindow.GetFromWindowId(windowId)` |
230
| `DisplayInformation.GetForCurrentView()` | Win32 `GetDpiForWindow()` or `XamlRoot.RasterizationScale` |
231
| `CoreApplication.GetCurrentView()` | Not available — track windows manually |
232
| `SystemNavigationManager.GetForCurrentView()` | Handle back navigation in `NavigationView` directly |
233
234
---
235
236
## Testing Migration
237
238
UWP unit test projects do not work with WinUI 3. You must migrate to the WinUI 3 test project templates.
239
240
| UWP | WinUI 3 |
241
|-----|---------|
242
| Unit Test App (Universal Windows) | **Unit Test App (WinUI in Desktop)** |
243
| Standard MSTest project with UWP types | Must use WinUI test app for Xaml runtime |
244
| `[TestMethod]` for all tests | `[TestMethod]` for logic, `[UITestMethod]` for XAML/UI tests |
245
| Class Library (Universal Windows) | **Class Library (WinUI in Desktop)** |
246
247
```csharp
248
// ✅ WinUI 3 unit test — use [UITestMethod] for any XAML interaction
249
[UITestMethod]
250
public void TestMyControl()
251
{
252
var control = new MyLibrary.MyUserControl();
253
Assert.AreEqual(expected, control.MyProperty);
254
}
255
```
256
257
**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.
258
259
---
260
261
## Migration Checklist
262
263
1. [ ] Replace all `Windows.UI.Xaml.*` using directives with `Microsoft.UI.Xaml.*`
264
2. [ ] Replace `Windows.UI.Colors` with `Microsoft.UI.Colors`
265
3. [ ] Replace `CoreDispatcher.RunAsync` with `DispatcherQueue.TryEnqueue`
266
4. [ ] Replace `Window.Current` with `App.MainWindow` static property
267
5. [ ] Add `XamlRoot` to all `ContentDialog` instances
268
6. [ ] Initialize all pickers with `InitializeWithWindow.Initialize(picker, hwnd)`
269
7. [ ] Replace `MessageDialog` with `ContentDialog`
270
8. [ ] Replace `ApplicationView`/`CoreWindow` with `AppWindow`
271
9. [ ] Replace `CoreApplicationViewTitleBar` with `AppWindowTitleBar`
272
10. [ ] Replace all `GetForCurrentView()` calls with `AppWindow` equivalents
273
11. [ ] Update interop for Share and Print managers
274
12. [ ] Replace `IBackgroundTask` with `AppLifecycle` activation
275
13. [ ] Update project file: TFM to `net10.0-windows10.0.22621.0`, add `<UseWinUI>true</UseWinUI>`
276
14. [ ] Migrate unit tests to **Unit Test App (WinUI in Desktop)** project; use `[UITestMethod]` for XAML tests
277
15. [ ] Test both packaged and unpackaged configurations
278

© GitHub, Inc. · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 10,805 prompts

More by @OpenSourceBuffGuy

See all
Felt miniature doll walking with a fox prompt image

Felt miniature doll walking with a fox

Generates a handmade felt miniature scene of a wool doll in a casual tech outfit walking across felt grass beside a small felt fox, in a macro stop-motion look.

image-prompt, nano-banana, embroidery, miniature, storybook, whimsical, cozy, macro, bokeh, natural-light, pastel, animals, toys, character-design, cinematic, for-artists, for-designers

Baby copies mother's yoga poses in 3D prompt image

Baby copies mother's yoga poses in 3D

Generates a 20-second Pixar-style 3D clip of a mother doing morning yoga while her baby copies every pose, with fixed character looks and camera plan.

video-prompt, animation, pixar-style, yoga, family, family-friendly, cinematic, character-consistency, camera-movement, tracking-shot, multi-shot, seedance, natural-light, soft-lighting, bokeh, pastel, close-up, wide-shot, widescreen, negative-prompt

Novelist & Book Architect (Genre-Aware) prompt image

Novelist & Book Architect (Genre-Aware)

Builds a full novel blueprint from a pitch: premise, comp titles, world bible, four characters, a 20-beat outline and a sample opening.

novel-writing, creative-writing, worldbuilding, character-development, act-as, for-writers, outline, structured-output, writing, storytelling, sci-fi, fantasy-world, romance, thriller, horror, publishing, editing, template, guardrails, advanced, books, literature

Reviews

Loading reviews…

Discussion

Loading comments…