Skip to content

Conversation

@ne0rrmatrix
Copy link
Member

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

  • Fixes #

PR Checklist

  • Has a linked Issue, and the Issue has been approved(bug) or Championed (feature/proposal)
  • Has tests (if omitted, state reason in description)
  • Has samples (if omitted, state reason in description)
  • Rebased on top of main at time of PR
  • Changes adhere to coding standard
  • Documentation created or updated: https://github.com/MicrosoftDocs/CommunityToolkit/pulls

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

…>. 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.
Copilot AI review requested due to automatic review settings January 9, 2026 23:43
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 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 VerticalStackLayout inside a ContentView in the StateContainer sample XAML to ensure proper collection handling by the XAML source generator

Copy link
Collaborator

@TheCodeTraveler TheCodeTraveler left a 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

Screenshot 2026-01-09 at 4 02 54 PM

@TheCodeTraveler TheCodeTraveler added the pending documentation This feature requires documentation label Jan 10, 2026
@VladislavAntonyuk
Copy link
Collaborator

Does the issue can be fixed if we change IList<View>to IList<IView>?

@ne0rrmatrix ne0rrmatrix requested a review from bijington January 16, 2026 01:42
@ne0rrmatrix
Copy link
Member Author

Does the issue can be fixed if we change IList<View>to IList<IView>?

I tried to do that. I had no luck making that work.

Copilot AI review requested due to automatic review settings January 16, 2026 01:43
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

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

@bijington
Copy link
Contributor

Does anyone know why this change fixes the issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pending documentation This feature requires documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants