New C# topic rules for .NET MAUI and Blazor, both centered on performance and cross-platform reuse. MAUI covers compilation strategy per platform (ReadyToRun, NativeAOT), trim and AOT safety, lean startup, CollectionView and flat layouts, and per-platform conventions. Blazor covers render mode cost, WebAssembly payload size and AOT trade-offs, prerender state, rendering cost, and Razor class libraries shared across web, WebAssembly, and Hybrid hosts. The C# index and global routing now name both. The existing C# rules gain the net11.0 default of C# 15, async void only for event handlers, explicit StringComparison, invariant culture for machine data, UTC or DateTimeOffset for stored times, ProblemDetails, and ValidateOnStart. Duplicate authorization and raw SQL guidance in the architecture rule is merged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3.8 KiB
3.8 KiB
C# .NET MAUI
Apply these rules when building or changing .NET MAUI apps. Performance and cross-platform behavior are the primary design goals. Check the project's TargetFrameworks first: runtimes, compilation defaults, and controls differ between MAUI 8, 9, 10, and 11. Load the UI/UX rules for interface design and the C# performance rule for measurement.
Performance
- Measure before optimizing, on real devices in
Releasebuilds: profile startup and hot paths withdotnet-traceor the MAUI profiling tooling, and compare against a baseline. Emulators andDebugbuilds do not show shipping performance. - Choose the compilation strategy per platform and record why: ReadyToRun (the
Releasedefault, withMauiEnableFullReadyToRunfor faster startup at a larger package), or NativeAOT for the smallest size and fastest startup where the app and its libraries are trim and AOT compatible. On Android, ReadyToRun requiresPublishTrimmed=true. - Keep the app trim and AOT safe: compiled bindings with
x:DataTypeeverywhere, no reflection-based serialization or DI scanning, and no trim warnings left unexplained. - Keep startup lean: register services in
MauiProgramwithout doing work there, defer non-essential initialization until after the first page renders, and load data asynchronously. - Use
CollectionView(not the deprecatedListView) with simple, flat item templates, and virtualize long lists. Avoid nested scroll views and per-item heavy converters or bindings. - Prefer flat
Gridlayouts over deeply nestedStackLayouttrees, useBorderinstead of the obsoleteFrame, and avoid fixed sizes that force re-measuring. - Size images for their display size, use platform-appropriate formats, and cache remote images. Never decode full-resolution images for thumbnails.
- Keep the main thread free: run I/O and CPU work off it, and marshal only UI updates back with
MainThread.BeginInvokeOnMainThreador the dispatcher. - Unsubscribe events and dispose resources when pages disappear, so navigation does not leak pages, view models, or native views.
Cross-platform
- Share UI, view models, and services by default. Put platform code in the
Platformsfolders or partial classes behind interfaces, and chooseOnPlatform,OnIdiom, or conditional compilation only for real platform differences. - Follow each target platform's conventions where people notice them: navigation, back behavior, tab placement, dialogs, and system fonts. Apply Apple's conventions to iOS and Mac Catalyst targets, Material to Android, and Windows conventions to Windows.
- Design for every supported size class and input: phone, tablet, and desktop windows, touch, mouse, and keyboard, safe areas, and text scaling.
- Customize controls through handlers and their mappers. Treat Xamarin custom renderers and compatibility renderers as temporary migration steps.
- Use MVVM with view models free of view types; prefer the project's MVVM library, such as CommunityToolkit.Mvvm source generators. Register pages and view models through dependency injection and navigate with Shell routes and typed parameters where the app uses Shell.
- Store tokens and other secrets with
SecureStorage, neverPreferences. WrapSecureStoragecalls in error handling and exclude its file from Android Auto Backup, because restored values cannot be decrypted on a new device. - Request runtime permissions through
Permissionsat the point of use, and keep the feature usable or explained when a permission is denied. - Expect suspension and termination: persist essential state in lifecycle events and restore it on resume.
- Set
SemanticProperties.DescriptionandSemanticProperties.Hinton meaningful images and controls without a visible label. - Build and test
Releasebuilds with trimming on every platform you ship. Trimming, AOT, and platform differences surface only there.