A new WPF topic rule covers accessibility (AutomationProperties names and help text, keyboard access, visible focus, high contrast through SystemColors, automation peers for custom controls), DPI scaling in device-independent units with tests across scale factors and monitors, and the Fluent theme through ThemeMode on .NET 9 and later. The existing WPF rules lose about a quarter of their size by merging one-line generic bullets that the C# and shared rules already cover, while keeping the WPF-specific pitfalls. Duplicate async void and binding error guidance is merged, client secret handling defers to the client app security rule, the architecture rule asks to check .NET Framework versus .NET before using newer APIs, and the CommunityToolkit note states that partial properties require C# 14. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
31 lines
2.2 KiB
Markdown
31 lines
2.2 KiB
Markdown
# WPF Architecture, Dependency Injection, Testing, and Performance
|
|
|
|
Apply these rules when structuring a WPF application, wiring dependency injection, testing view models, or optimizing performance. The C# rules and the shared refactoring rule cover general structure, testing, and code quality; this file adds WPF specifics.
|
|
|
|
## Architecture
|
|
|
|
* Check whether the app targets .NET Framework or .NET (8, 9, 10, or 11) before using newer APIs, and do not migrate between them as a side effect of another task. Use the C# version the project targets (.NET Framework 4.x defaults to C# 7.3).
|
|
* Prefer MVVM for non-trivial views and keep code-behind limited to view-specific behavior.
|
|
* Keep database, HTTP, and other external access behind focused services; never call them from views, controls, or directly from view models.
|
|
* Avoid static service access and global state.
|
|
|
|
## Dependency Injection
|
|
|
|
* Register views and view models as transient unless they hold intentionally shared state; register shared services as singletons only when they are thread-safe and stateless or own deliberately shared state.
|
|
* Do not resolve services from `App.Current` or a static container inside view models; inject them.
|
|
* Create EF Core contexts per operation through `IDbContextFactory<T>` (see the C# architecture rule).
|
|
|
|
## Testing
|
|
|
|
* Keep view models testable without a UI thread, and test domain and application logic independently from WPF.
|
|
* Avoid tests coupled to XAML structure, and keep UI automation tests to critical flows.
|
|
|
|
## Performance
|
|
|
|
Measure startup, UI responsiveness, memory, and rendering before optimizing, and work in this order: UI architecture, UI thread blocking, data loading and I/O, virtualization, visual tree complexity, binding and notification frequency, rendering and images, memory retention, allocations, then CPU micro-optimizations.
|
|
|
|
* Keep the UI thread responsive and virtualization enabled (see the collections rule).
|
|
* Keep visual trees shallow, and avoid unnecessary bindings, converters, resource lookups, property notifications, and full collection refreshes.
|
|
* Load large datasets incrementally, load images as the resources rule defines, and freeze `Freezable` objects where practical.
|
|
* Avoid unnecessary animations and high-frequency timers on the UI thread.
|