# 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 `Release` builds: profile startup and hot paths with `dotnet-trace` or the MAUI profiling tooling, and compare against a baseline. Emulators and `Debug` builds do not show shipping performance. * Choose the compilation strategy per platform and record why: ReadyToRun (the `Release` default, with `MauiEnableFullReadyToRun` for 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 requires `PublishTrimmed=true`. * Keep the app trim and AOT safe: compiled bindings with `x:DataType` everywhere, no reflection-based serialization or DI scanning, and no trim warnings left unexplained. * Keep startup lean: register services in `MauiProgram` without doing work there, defer non-essential initialization until after the first page renders, and load data asynchronously. * Use `CollectionView` (not the deprecated `ListView`) with simple, flat item templates, and virtualize long lists. Avoid nested scroll views and per-item heavy converters or bindings. * Prefer flat `Grid` layouts over deeply nested `StackLayout` trees, use `Border` instead of the obsolete `Frame`, 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.BeginInvokeOnMainThread` or 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 `Platforms` folders or partial classes behind interfaces, and choose `OnPlatform`, `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`, never `Preferences`. Wrap `SecureStorage` calls 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 `Permissions` at 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.Description` and `SemanticProperties.Hint` on meaningful images and controls without a visible label. * Build and test `Release` builds with trimming on every platform you ship. Trimming, AOT, and platform differences surface only there.