-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Android, iOS] Throw exceptions consistently for invalid StaticResource references to prevent relaunch crashes #33859
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
base: main
Are you sure you want to change the base?
Conversation
…es on iOS only when not debugging
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 aims to fix issue #23903 where apps crash on Android and iOS when relaunching with invalid StaticResource references (e.g., missing ControlTemplate). The proposed solution changes the exception handling behavior to always throw exceptions for invalid StaticResource references, regardless of whether an exception handler is present.
Changes:
- Modified StaticResourceExtension to create the exception once, optionally log it through the exception handler if present, then always throw it (previously returned null when handler was present)
- Updated ApplyPropertiesVisitor to always rethrow exceptions after logging them to the exception handler (previously only threw if no handler was present)
- Added a test to verify that exceptions are thrown even when ExceptionHandler2 is set
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/Controls/src/Xaml/MarkupExtensions/StaticResourceExtension.cs | Changed to always throw XamlParseException for missing resources after optionally logging to ExceptionHandler2, instead of returning null when handler is present |
| src/Controls/src/Xaml/ApplyPropertiesVisitor.cs | Modified exception handling to always rethrow after invoking exception handler, removing the conditional logic that only threw when no handler was present |
| src/Controls/tests/DeviceTests/Xaml/StaticResourceTests.cs | Added test to verify missing StaticResource throws exception even when ExceptionHandler2 is set |
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!
Issue Details
When an invalid template name is provided, the app crashes on Android and iOS when relaunching the app.
Root Cause
Inconsistent exception handling in StaticResourceExtension - it returned null when an exception handler was present but threw an exception when debugger is not attached.
Description of Change
Modified StaticResourceExtension.cs and ApplyPropertiesVisitor.cs to always throw exceptions for invalid StaticResource references, regardless of whether an exception handler is present. Previously, the code returned null and allowing the app to continue. The fix ensures consistent behavior by creating the exception once, optionally logging it through the exception handler if available, and then always throwing it.
Tested the behavior in the following platforms
Issues Fixed
Fixes #23903
Screenshots
iOS:
23903BeforeiOS.mov
23903AfteriOS.mov
Android:
23903AndroidBefore.mov
23903AndroidAfter.mov