Fix loading ResourceDictionary from source#31435
Merged
simonrozsival merged 3 commits intonet10.0from Sep 2, 2025
Merged
Conversation
…nary constructor throws
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a bug where loading ResourceDictionary from Source would swallow exceptions thrown during XAML inflation. The fix ensures that when a TargetInvocationException is caught, the inner exception is properly re-thrown to provide meaningful error messages to developers.
- Wraps ResourceDictionary instantiation in try-catch blocks to handle
TargetInvocationException - Uses
ExceptionDispatchInfo.Capture()to preserve the original stack trace when re-throwing inner exceptions - Adds comprehensive unit tests to verify that XAML parse exceptions are properly reported
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/Controls/src/Core/ResourceDictionary.cs | Core fix that wraps ResourceDictionary instantiation with proper exception handling |
| src/Controls/tests/Xaml.UnitTests/Issues/Maui31308.xaml | Test page that references a ResourceDictionary with an invalid StaticResource |
| src/Controls/tests/Xaml.UnitTests/Issues/Maui31308.xaml.cs | Unit test class that verifies the XamlParseException is properly thrown with the correct message |
| src/Controls/tests/Xaml.UnitTests/Issues/Maui31308_Style.xaml | ResourceDictionary with an intentionally invalid StaticResource reference for testing |
| src/Controls/tests/Xaml.UnitTests/Issues/Maui31308_Style.xaml.cs | Code-behind for the test ResourceDictionary |
StephaneDelcroix
previously requested changes
Sep 1, 2025
| public void MissingResourceDictionaryValueIsReported([Values] XamlInflator inflator) | ||
| { | ||
| var exception = Assert.Catch<XamlParseException>(() => new Maui31308(inflator)); | ||
| Assert.AreEqual("Position 6:38. StaticResource not found for key ThisKeyDoesNotExistInAnyResourceDictionary", exception.Message); |
Contributor
There was a problem hiding this comment.
I wish we could catch that at build time...
vitek-karas
approved these changes
Sep 1, 2025
StephaneDelcroix
approved these changes
Sep 2, 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.
Description of Change
This PR fixes a bug where loading ResourceDictionary from Source will swallow any exceptions thrown during RD inflation. Whenever we catch
TargetInvocationException, we re-throw the inner exception.Issues Fixed
Fixes #31308