-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Fix loading ResourceDictionary from source #31435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix loading ResourceDictionary from source #31435
Conversation
…nary constructor throws
There was a problem hiding this comment.
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 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 |
| 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wish we could catch that at build time...
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