Repository navigation
[XC] Fix XamlC null key error in CanAddToResourceDictionary - #32902
Merged
Merged
Conversation
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.
Contributor
There was a problem hiding this comment.
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
CanAddToResourceDictionaryto preventArgumentNullExceptionwhen 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 > |
There was a problem hiding this comment.
Remove extra space before the closing angle bracket. Should be <DataTemplate> instead of <DataTemplate >.
Suggested change
| <DataTemplate > | |
| <DataTemplate> |
simonrozsival
approved these changes
Nov 28, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.,
CollectionViewwithx:Arrayitems andDataTemplate), XamlC could throwValue cannot be null. (Parameter 'key')becauseCanAddToResourceDictionarywas called with a nullparentparameter.Root Cause
The error occurs when
context.Cache.GetResourceNamesInUse(parent)is called with a nullVariableDefinition. This happens becauseGetResourceNamesInUseinternally uses aDictionary<VariableDefinition, ICollection<string>>, and accessing a dictionary with a null key throwsArgumentNullException("key").Fix
Added an early null check in
CanAddToResourceDictionaryto returnfalseifparentis null. This is safe because:parentis null, we cannot add to a ResourceDictionary anywayfalseallows the compiler to try other property-setting methodsChanges
parentparameterCollectionView,x:Array, andDataTemplateTesting
Maui24472verifies the fix works for all XamlInflator modes (Runtime, XamlC, SourceGen)