maui-performance
22
总安装量
20
周安装量
#17081
全站排名
安装命令
npx skills add https://github.com/davidortinau/maui-skills --skill maui-performance
Agent 安装分布
gemini-cli
18
codex
18
opencode
18
github-copilot
17
amp
17
kimi-cli
17
Skill 文档
.NET MAUI Performance Optimization
Profiling
- dotnet-trace: Cross-platform profiler for all MAUI targets. Collect traces with:
dotnet-trace collect --process-id <PID> --providers Microsoft-DotNETCore-SampleProfiler - PerfView (Windows only): Use for deep allocation and CPU analysis on WinUI targets.
- Profile before optimizing â measure, don’t guess.
Compiled Bindings
- Always set
x:DataTypeon pages/views to enable compiled bindings. - Compiled bindings are 8â20Ã faster than reflection-based bindings.
- Required for NativeAOT and full trimming â reflection bindings break under trimming.
- Apply
x:DataTypeat the highest reasonable scope (page or root element).
<ContentPage xmlns:vm="clr-namespace:MyApp.ViewModels"
x:DataType="vm:MainViewModel">
<Label Text="{Binding Title}" />
</ContentPage>
- For
DataTemplateinsideCollectionView/ListView, setx:DataTypeon the template root:
<DataTemplate x:DataType="model:Item">
<Label Text="{Binding Name}" />
</DataTemplate>
- Minimize unnecessary bindings: Static content that never changes doesn’t need
{Binding}. Use literal values orx:Staticinstead.
Layout Optimization
- Avoid nested single-child layouts â a
StackLayoutwith one child is pointless overhead. - Prefer
Gridover nestedStackLayout/VerticalStackLayoutâ Grid resolves layout in a single pass. - Use
IsVisible="False"to hide elements rather than adding/removing from the tree at runtime. - Remove unnecessary wrapper containers â every layout adds a measure/arrange pass.
- Avoid
AbsoluteLayoutwhenGridwith row/column definitions achieves the same result. - Set explicit
WidthRequest/HeightRequestwhere sizes are known to reduce measure passes.
CollectionView and ListView
- Use
ItemSizingStrategy="MeasureFirstItem"onCollectionViewwhen items have uniform height â avoids measuring every item individually.
<CollectionView ItemSizingStrategy="MeasureFirstItem"
ItemsSource="{Binding Items}">
- Prefer
CollectionViewoverListViewfor new development. - Keep
DataTemplatevisual trees shallow â fewer nested layouts per item.
Image Optimization
- Size images to display resolution â don’t load a 4000Ã3000 image for a 200Ã200 display.
- Use
Aspect="AspectFill"orAspectFitand set explicit dimensions. - Cache downloaded images:
CachingEnabled="True"(default) onUriImageSource, setCacheValidity. - Use platform-appropriate formats (WebP for Android, HEIF/PNG for iOS).
- For icons and simple graphics, prefer SVG or vector-based font icons.
Resource Dictionaries
- App-level (
App.xaml): Shared styles, colors, converters used across multiple pages. - Page-level: Styles/resources specific to a single page â keeps App.xaml lean.
- Avoid duplicating resources across dictionaries.
- Use
MergedDictionariesto organize large resource sets without bloating a single file.
Startup Optimization
- Minimize work in
Appconstructor andMainPageconstructor. - Defer non-essential service registration â use lazy initialization where possible.
- Reduce the number of XAML pages parsed at startup (use
Shellrouting for deferred loading). - Remove unused
Handlers/Effectsregistrations fromMauiProgram.cs. - On Android, ensure
AndroidManifest.xmldoesn’t force unnecessary permissions checks at launch.
Trimming
Enable IL trimming to reduce app size and remove unused code:
<PropertyGroup>
<PublishTrimmed>true</PublishTrimmed>
<TrimMode>full</TrimMode>
</PropertyGroup>
- Test thoroughly â trimming can remove code accessed only via reflection.
- Use
[DynamicDependency]or[UnconditionalSuppressMessage]to preserve reflection targets. - Compiled bindings are essential â reflection-based bindings will be trimmed away.
NativeAOT
- NativeAOT compiles to native code ahead-of-time for faster startup and smaller footprint.
- Requirements: compiled bindings, no unconstrained reflection, trimming-compatible code.
<PropertyGroup>
<PublishAot>true</PublishAot>
<PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>
- Audit third-party NuGet packages for trimming/AOT compatibility.
- Use
[JsonSerializable]source generators instead of reflection-based JSON serialization. - Run
dotnet publishwith AOT to verify no warnings before shipping.
Quick Checklist
- â
x:DataTypeon all pages, templates, and data-bound views - â No nested single-child layouts
- â Grid preferred over nested StackLayouts
- â Images sized to display, caching enabled
- â
ItemSizingStrategy="MeasureFirstItem"on uniform CollectionViews - â Startup work minimized and deferred
- â Trimming enabled and tested
- â Profile with dotnet-trace before and after changes