Skip to content

Native AOT: InvalidCastException when using GridSplitter #612

Open

Description

@tipa

Describe the bug

I'm seeing this crash reported on a device with Windows 10.0.19045. I wasn't able to reproduce it locally on a Windows 11 machine

System.InvalidCastException: Specified cast is not valid
   at System.Runtime.RuntimeExports.RhUnbox2(MethodTable*, Object) + 0x3f
   at CommunityToolkit.WinUI.Controls.GridSplitter.IsStarColumn(ColumnDefinition) + 0x53
   at CommunityToolkit.WinUI.Controls.GridSplitter.OnDragHorizontal(Double) + 0xf9
   at CommunityToolkit.WinUI.Controls.SizerBase.OnManipulationDelta(ManipulationDeltaRoutedEventArgs) + 0x11b
   at ABI.Microsoft.UI.Xaml.Controls.IControlOverrides.Do_Abi_OnManipulationDelta_15(IntPtr, IntPtr) + 0x58

I'm using CommunityToolkit.WinUI.Controls.Sizers 8.2.241112-preview1 in a .NET9 WinUI3 app that is compiled using Native AOT

Steps to reproduce

I'm essentially only using a GridSplitter and adjusting Grid.Row and Grid.Column in code

<controls:GridSplitter x:Name="gridSplitter" Grid.Row="1" Grid.Column="1" />

Expected behavior

No crash

Screenshots

No response

Code Platform

  • UWP
  • WinAppSDK / WinUI 3
  • Web Assembly (WASM)
  • Android
  • iOS
  • MacOS
  • Linux / GTK

Windows Build Number

  • Windows 10 1809 (Build 17763)
  • Windows 10 1903 (Build 18362)
  • Windows 10 1909 (Build 18363)
  • Windows 10 2004 (Build 19041)
  • Windows 10 20H2 (Build 19042)
  • Windows 10 21H1 (Build 19043)
  • Windows 10 21H2 (Build 19044)
  • Windows 10 22H2 (Build 19045)
  • Windows 11 21H2 (Build 22000)
  • Other (specify)

Other Windows Build number

No response

App minimum and target SDK version

  • Windows 10, version 1809 (Build 17763)
  • Windows 10, version 1903 (Build 18362)
  • Windows 10, version 1909 (Build 18363)
  • Windows 10, version 2004 (Build 19041)
  • Windows 10, version 2104 (Build 20348)
  • Windows 11, version 22H2 (Build 22000)
  • Other (specify)

Other SDK version

No response

Visual Studio Version

2022

Visual Studio Build Number

No response

Device form factor

No response

Additional context

No response

Help us help you

Yes, I'd like to be assigned to work on this item.

Activity

  1. changed the title [-]InvalidCastException when using GridSplitter[/-] [+]Native AOT: InvalidCastException when using GridSplitter[/+] on Jan 26, 2025
  2. Sergio0694 commented on Jan 28, 2025

    @Sergio0694
    Member

    @tipa can you try again with the latest WCT 8.2-preview2 build that went out today, and CsWinRT 2.2.0? Thank you!

  3. tipa commented on Jan 28, 2025

    @tipa
    Author

    Still crashes with WCT 8.2-preview2 & CsWinRT 2.2.0.

    But adding line resolves it for me:
    [assembly: GeneratedWinRTExposedExternalType(typeof(GridLength))]

  4. added this to the 8.2 milestone on Feb 3, 2025
  5. michael-hawker commented on Feb 3, 2025

    @michael-hawker
    Member

    @Sergio0694 so since GridLength is a system type is this an issue in CsWinRT or do we need to annotate something in the Toolkit?

  6. Sergio0694 commented on Feb 3, 2025

    @Sergio0694
    Member

    This feels like a CsWinRT issue, possibly. cc. @manodasanW

  7. added
    nativeaot
    on Feb 6, 2025
  8. moved this to 🆕 New in Toolkit 8.xon Feb 6, 2025
  9. michael-hawker commented on Feb 7, 2025

    @michael-hawker
    Member

    Talked quick to @manodasanW and seems like it may smell like a C#/WinRT issue.

    @tipa do you have a small minimal reproduction project of the issue you could zip up? If so, would you mind opening an issue over there, attaching it, and linking back to this one?

    I did see this GridLength issue over there, but not sure if related, seems different? microsoft/CsWinRT#1417

  10. tipa commented on Feb 7, 2025

    @tipa
    Author
  11. added
    components::controls::sizers
    on Aug 7, 2025
  12. Furry-Xiyi commented on May 15, 2026

    @Furry-Xiyi

    我很快和他们沟通过,感觉可能有C#/WinRT的问题。

    你有没有一个小型的复制项目可以拉链拉上?如果有的话,你介意在那边打开一期,附上并链接回这期吗?

    我确实看到过那边的网格长度问题,但不确定是否相关,感觉不一样?microsoft/CsWinRT#1417

    你好 当我尝试在打包(AOT)安装完成后拖动也发生了崩溃(VS里的Release+AOT一切正常) 但是我使用AI修复了这个问题 关键是在于GridSplitter.Helpers.cs中的IsStarColumn 和 IsStarRow 里用 GetValue 返回 object,然后强转 (GridLength) —— 这就是装箱/拆箱操作,在 .NET Native AOT 下 GridLength 的 marshaling 元数据被裁掉后直接崩溃。

  13. Furry-Xiyi commented on May 15, 2026

    @Furry-Xiyi

    我很快和他们沟通过,感觉可能有C#/WinRT的问题。

    你有没有一个小型的复制项目可以拉链拉上?如果有的话,你介意在那边打开一期,附上并链接回这期吗?

    我确实看到过那边的网格长度问题,但不确定是否相关,感觉不一样?microsoft/CsWinRT#1417

    改成private static bool IsStarColumn(ColumnDefinition definition)
    {
    return definition.Width.IsStar;
    }

    private static bool IsStarRow(RowDefinition definition)
    {
    return definition.Height.IsStar;
    }即可

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    components::controls::sizersnativeaot

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions