Skip to content

[XC] Fix XamlC null key error in CanAddToResourceDictionary - #32902

Merged
StephaneDelcroix merged 1 commit into
mainfrom
fix/24472-xamlc-null-key
Nov 28, 2025
Merged

StephaneDelcroix merged 1 commit into
mainfrom
fix/24472-xamlc-null-key

Conversation

@StephaneDelcroix

Copy link
Copy Markdown
Contributor

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Description

Fixes #24472

When processing XAML with complex nested structures (e.g., CollectionView with x:Array items and DataTemplate), XamlC could throw Value cannot be null. (Parameter 'key') because CanAddToResourceDictionary was called with a null parent parameter.

Root Cause

The error occurs when context.Cache.GetResourceNamesInUse(parent) is called with a null VariableDefinition. This happens because GetResourceNamesInUse internally uses a Dictionary<VariableDefinition, ICollection<string>>, and accessing a dictionary with a null key throws ArgumentNullException("key").

Fix

Added an early null check in CanAddToResourceDictionary to return false if parent is null. This is safe because:

  1. If parent is null, we cannot add to a ResourceDictionary anyway
  2. Returning false allows the compiler to try other property-setting methods

Changes

  • src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs: Added null check for parent parameter
  • src/Controls/tests/Xaml.UnitTests/Issues/Maui24472.xaml/.cs: Added unit test with CollectionView, x:Array, and DataTemplate

Testing

  • All 1755 XAML unit tests pass
  • New test Maui24472 verifies the fix works for all XamlInflator modes (Runtime, XamlC, SourceGen)

Fixes #24472

When processing XAML with complex nested structures (e.g., CollectionView
with x:Array items and DataTemplate), XamlC could throw 'Value cannot be
null. (Parameter 'key')' because CanAddToResourceDictionary was called
with a null parent parameter.

The fix adds an early null check in CanAddToResourceDictionary to return
false if parent is null, allowing the compiler to try other property-setting
methods instead of crashing.

Added unit test Maui24472 to verify XAML with CollectionView, x:Array,
and DataTemplate compiles correctly.
Copilot AI review requested due to automatic review settings November 28, 2025 09:43
@StephaneDelcroix StephaneDelcroix changed the title Fix XamlC null key error in CanAddToResourceDictionary [XC] Fix XamlC null key error in CanAddToResourceDictionary Nov 28, 2025

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes issue #24472 where XamlC throws a ArgumentNullException with message "Value cannot be null. (Parameter 'key')" when compiling XAML with complex nested structures involving CollectionView, x:Array items, and DataTemplate. The fix adds an early null check in the CanAddToResourceDictionary method to gracefully handle cases where the parent parameter is null.

Key Changes

  • Added null guard in CanAddToResourceDictionary to prevent ArgumentNullException when accessing dictionary with null key
  • Added comprehensive XAML test case that reproduces the original issue
  • Test verifies the fix works across all XAML inflation modes (Runtime, XamlC, SourceGen)

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs Added null check for parent parameter in CanAddToResourceDictionary method
src/Controls/tests/Xaml.UnitTests/Issues/Maui24472.xaml XAML test case with CollectionView, x:Array, and DataTemplate that previously crashed XamlC
src/Controls/tests/Xaml.UnitTests/Issues/Maui24472.xaml.cs Test fixture verifying the fix works for all XamlInflator modes

</Grid>
</CollectionView.Header>
<CollectionView.ItemTemplate>
<DataTemplate >

Copilot AI Nov 28, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra space before the closing angle bracket. Should be <DataTemplate> instead of <DataTemplate >.

Suggested change
<DataTemplate >
<DataTemplate>

Copilot uses AI. Check for mistakes.
@StephaneDelcroix
StephaneDelcroix merged commit baa9851 into main Nov 28, 2025
@StephaneDelcroix
StephaneDelcroix deleted the fix/24472-xamlc-null-key branch November 28, 2025 18:41
@StephaneDelcroix StephaneDelcroix added this to the .NET 10.0 SR2 milestone Dec 2, 2025
@github-actions github-actions Bot locked and limited conversation to collaborators Jan 2, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XamlC error : Value cannot be null. (Parameter 'key')

3 participants