Skip to content

Conversation

@simonrozsival
Copy link
Member

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

@simonrozsival simonrozsival requested a review from a team as a code owner September 1, 2025 14:21
@simonrozsival simonrozsival changed the title Fix Fix loading ResourceDictionary from source Sep 1, 2025
Copy link
Contributor

Copilot AI left a 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);
Copy link
Contributor

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...

@simonrozsival simonrozsival merged commit 8f1136b into net10.0 Sep 2, 2025
149 checks passed
@simonrozsival simonrozsival deleted the dev/srozsival/fix-rd-target-invocation-exception branch September 2, 2025 10:41
@github-actions github-actions bot locked and limited conversation to collaborators Oct 3, 2025
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.

4 participants