-
Notifications
You must be signed in to change notification settings - Fork 472
Fix StateContainer SourceGen mishandling Xaml with invalid cast. #3033
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?
Fix StateContainer SourceGen mishandling Xaml with invalid cast. #3033
Conversation
…>. In XAML you used a single VerticalStackLayout as the content of <mct:StateContainer.StateViews>. The XAML generator emitted code that assigns that single element directly to the attached property (a SetValue(..., (IList<View>)verticalStackLayout)), i.e. it treats the element as the collection itself. VerticalStackLayout does not implement IList<Microsoft.Maui.Controls.View> (its children is IList<IView>), so the explicit runtime cast fails. Root cause: XAML codegen mis-handles the single child that is an enumerable/layout and emits an invalid cast (or the XAML usage triggers the generator to assign the element instead of adding it as an item). This is a codegen/XAML usage mismatch, not a runtime bug in StateContainer itself.
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 System.InvalidCastException that occurs when defining StateViews in XAML with a VerticalStackLayout as the direct child of the StateContainer.StateViews attached property. The XAML compiler was attempting to cast the VerticalStackLayout directly to IList<View>, which is invalid since VerticalStackLayout does not implement this interface.
Key Changes:
- Wrapped the
VerticalStackLayoutinside aContentViewin the StateContainer sample XAML to ensure proper collection handling by the XAML source generator
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.
Thanks James! I confirm that this fixes the runtime crash uncovered by enabling the XAML Source Generator.
A big concern I have is that the StateContainer Documentation also recommends putting a VerticalStackLayout directly into <mct:StateContainer.StateViews> which was causing the runtime crash in our sample app after enabling XAML Source Generator.
Could you please work with @bijington to determine if this is a documentation issue or whether we have a deeper problem in our implementation of StateContainer.StateViews?
I'm not a XAML expert, but perhaps there's a solution where we use [ContentProperty]? I have no idea, so I'll defer to you guys who know more XAML than me!
For now, I'm marking my review as Request Changes until we either have a PR submitted to update the Docs or we do something in code to make StateContainer.StateViews more compatible with our documented recommendations.
StateContainer Docs
Here's where we are currently recommending putting a VerticalStackLayout directly into <mct:StateContainer.StateViews> in our docs: https://learn.microsoft.com/en-us/dotnet/communitytoolkit/maui/layouts/statecontainer
|
Does the issue can be fixed if we change |
I tried to do that. I had no luck making that work. |
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
Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.
|
Does anyone know why this change fixes the issue? |
Summary
This PR fixes a System.InvalidCastException occurring when defining StateViews in XAML. The crash was caused by the XAML compiler attempting to directly cast a VerticalStackLayout to IList<Microsoft.Maui.Controls.View> when assigned as the immediate child of the StateContainer.StateViews attached property.
Description of Change
The root cause was a mismatch between the XAML usage and how the code generator handles attached properties of collection types.
StateContainer.StateViewsProperty is defined as an IList. When a single layout (like VerticalStackLayout) is placed directly inside the mct:StateContainer.StateViews tags, the XAML generator emits code that attempts to assign that single element directly to the property via a cast:
SetValue(StateContainer.StateViewsProperty, (IList)verticalStackLayout).
Because VerticalStackLayout does not implement IList (its Children property is an IList), this explicit cast fails at runtime. By wrapping the content or explicitly defining the collection items, we ensure the XAML generator treats the elements as items to be added to the list rather than the list instance itself.
Technical Details
Issue: System.InvalidCastException during XAML initialization.
Fix: Updated XAML structure to ensure proper collection population, avoiding the invalid cast emitted by the XAML compiler.
Note: This is a XAML usage/codegen mismatch rather than a logic bug within the StateContainer implementation.
Linked Issues
PR Checklist
approved(bug) orChampioned(feature/proposal)mainat time of PRAdditional information
We switched on xaml source generation in a previous PR. The error when navigating to StateContainer page is possibly a bug in Maui and I will be contacting someone who can take a look at it hopefully. I will update this PR with info as I get it.