Diagnosing Xamarin.Forms XAML Compilation Failures
Diagnose Xamarin.Forms XAML compilation failures using build output, attribute placement, and the Skip isolation option.
03 Jul 2025, 02:32 UTC

Problem: XAML errors surface late or not at all
A Xamarin.Forms page can build successfully but fail at runtime when it is navigated to, with errors about missing elements, properties, or binding paths. Without compile-time validation, Xamarin.Forms parses XAML while the app runs, so these problems stay hidden until the user reaches the affected page. A related issue is a build that passes in Debug but fails in Release with XAML compilation errors.
Useful takeaway: enable compile-time XAML validation in both Debug and Release, read XamlC errors directly from build output, and use the Skip option to confirm whether compilation is causing a page failure before rewriting XAML.
Recognizable conditions
- The build succeeds, but navigating to a page throws a runtime XAML parse exception or missing member exception.
- The Release build fails with XAML compilation errors while Debug succeeds.
- A page works when compilation is disabled but fails when it is enabled.
Cause and diagnostic table
| Observation | Likely cause | Primary check |
|---|---|---|
| Build output contains a XAMLC error with a file and line number | Invalid XAML under compilation | Search the build log for XamlC |
| Errors occur only in Release | Compilation is enabled only for Release, or the linker removed a type referenced from XAML | Compare attribute placement across configurations and review linker behavior |
| A page builds cleanly but fails at runtime | XAML compilation is disabled for that page or assembly | Inspect XamlCompilation attribute placement |
Ordered checks
-
Confirm where XAML compilation is enabled
Open AssemblyInfo.cs, typically under Properties or Shared depending on project layout. XamlCompilation can be applied at assembly, namespace, or class level. Assembly-wide compilation looks like this:
[assembly: XamlCompilation(XamlCompilationOptions.Compile)]Individual pages can opt in with a class-level attribute:
[XamlCompilation(XamlCompilationOptions.Compile)] public partial class MainPage : ContentPage { ... }Compile turns XAML into binary IL during the build; Skip leaves parsing for runtime.
-
Use the same setting in Debug and Release
Assembly attributes normally apply to every configuration, but some projects wrap them in #if RELEASE. Deferred errors make release validation slower and hide problems until late in the pipeline. Keep Compile enabled in both configurations when possible.
-
Rebuild with detailed build output
Run from the development machine or build agent that can restore NuGet packages and resolve the Xamarin SDKs:
msbuild YourSolution.sln /p:Configuration=Release /p:Verbosity=DetailedPermissions: standard user access to the project folder is sufficient; elevated rights are only needed if the build writes to protected locations. Expected check: the log contains XamlC task entries. Risk: Diagnostic verbosity produces very large logs, so start with Detailed. Search the output for "XamlC". A failed compile emits errors with the XAML file and line number, such as an invalid element name or an unresolvable binding path.
-
Isolate a suspect page
If a page fails at runtime and compilation is suspected, temporarily change its class-level attribute to Skip:
[XamlCompilation(XamlCompilationOptions.Skip)] public partial class SuspectPage : ContentPage { ... }Rebuild and run. If the page works with Skip but fails with Compile, the failure is related to compiled XAML handling, such as an unsupported markup extension or dynamic resource lookup, rather than view-model logic.
-
Check linker preservation
The IL linker can remove types that are referenced only from compiled XAML. If a page works before linking but breaks after, add a linker description file or mark the type with [Preserve]. This is most relevant for linked Release builds.
Fixes tied to findings
- XAMLC error in build output: Correct the flagged XAML. Common fixes include correcting element names, adding missing xmlns declarations, and fixing binding paths. Rebuild after each change.
- Compilation disabled: Add the assembly attribute to AssemblyInfo.cs or add the class-level attribute to pages that need compile-time validation.
- Release-only failure: Remove conditional compilation around the attribute so Debug and Release match, and review linker settings.
- Unsupported XAML construct: Leave the page on Skip as a short-term workaround, rewrite the XAML using supported constructs, or move logic to code-behind.
Escalation criteria
- XamlC errors persist after correcting the reported XAML line, or the build log lacks enough detail to locate the problem.
- A page requires a markup extension or dynamic resource that compiled XAML does not handle and no supported rewrite is available.
- The linker removes types despite [Preserve], and the failure appears only in linked Release builds.
Capture a Diagnostic verbosity build log and the minimal XAML reproduction before engaging platform support or planning migration of the affected UI to .NET MAUI, the supported successor to Xamarin.Forms.
Limitations and verification
XAML compilation is available in Xamarin.Forms 4.x and 5.x. It does not apply to Xamarin.iOS or Xamarin.Android native UI, and this guidance does not cover .NET MAUI. Compile-time validation catches structural XAML problems; it does not catch logic errors in data templates or converters that only appear with real data.
Practical check: rebuild with Detailed verbosity, confirm XamlC runs without errors, then navigate to each previously failing page in both Debug and Release. If pages load without runtime XAML exceptions, the configuration is working.
Rollback
Enabling XamlCompilation changes source code. To roll back, remove the assembly attribute from AssemblyInfo.cs or revert the class-level attribute to Skip, then rebuild. Because this is a text change in source control, reverting the commit restores the previous behavior.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.