Problem
When a consumer references an icon font package and uses glyphs in XAML/C#, but forgets to call .UseIconFonts() (or the per-font .UseBootstrapIcons() etc.) on their MauiAppBuilder, the fonts won't render at runtime. The IMauiInitializeService fallback may catch this, but it's still a confusing experience.
Proposed Solution
Ship a Roslyn DiagnosticAnalyzer alongside the source generator that detects this at compile time.
Detection logic
- Scan for methods returning
MauiApp (the standard CreateMauiApp() pattern)
- Check if any call to
UseIconFonts() or a per-font Use{FontClass}() method exists
- If the icon font package is referenced but no initialization call is found → emit warning
ICONFONT001: Icon font package is referenced but UseIconFonts() was not called on MauiAppBuilder.
Add .UseIconFonts() to your MauiAppBuilder in MauiProgram.cs.
Severity
DiagnosticSeverity.Warning — not an error, since the IMauiInitializeService fallback will still register the fonts. This is a best-practice nudge.
Approach Options
| Approach |
Pros |
Cons |
| Roslyn Analyzer (recommended) |
Compile-time, zero runtime cost, ships in same NuGet |
Cannot inspect XAML directly, only C# syntax trees |
Runtime #if DEBUG check |
Simple, catches it at startup |
Only at runtime, requires app to run |
Static flag in UseIconFonts() |
Very simple |
Only runtime, same limitation |
The Roslyn Analyzer approach is preferred. It can be scoped to: "if the project has any IconFontDefinition items (or MauiFont items with the generator) and no UseIconFonts() call exists anywhere in the assembly, emit the warning."
Packaging
The analyzer DLL ships in the same NuGet package as the source generator (in the analyzers/ folder). No additional package reference needed.
References
Problem
When a consumer references an icon font package and uses glyphs in XAML/C#, but forgets to call
.UseIconFonts()(or the per-font.UseBootstrapIcons()etc.) on theirMauiAppBuilder, the fonts won't render at runtime. TheIMauiInitializeServicefallback may catch this, but it's still a confusing experience.Proposed Solution
Ship a Roslyn
DiagnosticAnalyzeralongside the source generator that detects this at compile time.Detection logic
MauiApp(the standardCreateMauiApp()pattern)UseIconFonts()or a per-fontUse{FontClass}()method existsSeverity
DiagnosticSeverity.Warning— not an error, since theIMauiInitializeServicefallback will still register the fonts. This is a best-practice nudge.Approach Options
#if DEBUGcheckUseIconFonts()The Roslyn Analyzer approach is preferred. It can be scoped to: "if the project has any
IconFontDefinitionitems (orMauiFontitems with the generator) and noUseIconFonts()call exists anywhere in the assembly, emit the warning."Packaging
The analyzer DLL ships in the same NuGet package as the source generator (in the
analyzers/folder). No additional package reference needed.References