You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ListView绑定自定义类/记录对象时启动失败,抛出类型转换异常

绑定自定义类到ListView时抛出InvalidCastException的解决办法

当我把ListView的ItemsSource绑定为字符串列表时一切正常,但绑定自定义Data类对象列表时,应用无法启动,一段时间后抛出以下异常:

System.InvalidCastException: Specified cast is not valid

我的XAML代码:

<ListView x:Name="Posts">
    <ListView.ItemTemplate>
        <DataTemplate>
            <VerticalStackLayout Spacing="25" Margin="10,0" VerticalOptions="Center">
                <Frame BackgroundColor="#191919">
                    <VerticalStackLayout>
                        <VerticalStackLayout Margin="5">
                            <Label Text="{Binding Name}" FontSize="18" TextColor="Gray" />
                            <Label Text="{Binding Title}" FontSize="Large" FontAttributes="Bold" />
                        </VerticalStackLayout>
                        <Border>
                            <Border.StrokeShape>
                                <RoundRectangle CornerRadius="5" />
                            </Border.StrokeShape>
                            <Image MinimumHeightRequest="200" MaximumHeightRequest="350" Aspect="AspectFill" Source="{Binding Image}" />
                        </Border>
                        <HorizontalStackLayout Margin="0,5,0,0">
                            <Button ImageSource="upvote.png" BackgroundColor="Transparent" />
                            <Label Text="{Binding Votes}" FontSize="Medium" Margin="0, 10, 0, 0" TextColor="Gray" />
                            <Button ImageSource="downvote.png" BackgroundColor="Transparent" />
                        </HorizontalStackLayout>
                    </VerticalStackLayout>
                </Frame>
            </VerticalStackLayout>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

绑定逻辑代码:

List<Data> items = new() { new Data("User", "Lorem ipsum dolor sit amet", "21", "<Image url>") };
Posts.ItemsSource = items;

问题排查与解决

  • 检查Data类的属性访问权限
    数据绑定要求绑定的属性必须是**公共(public)**且带有get访问器。如果你的Data类属性是私有或内部访问级别,绑定系统无法获取属性值,就会抛出转换异常。

    错误示例:

    class Data
    {
        string Name; // 无访问修饰符,默认internal
        // 其他属性同理
    }
    

    正确定义:

    public class Data
    {
        public string Name { get; }
        public string Title { get; }
        public string Votes { get; }
        public string Image { get; }
    
        public Data(string name, string title, string votes, string image)
        {
            Name = name;
            Title = title;
            Votes = votes;
            Image = image;
        }
    }
    
  • 确认属性类型与绑定数据匹配
    如果Votes属性定义为int类型,但你构造函数传入的是字符串"21",绑定过程中会尝试将字符串转为整数,可能触发转换异常。确保属性类型和传入的数据类型一致:

    • 若用字符串存储投票数,Votes设为string;
    • 若用数字存储,构造函数传入整数(比如21而非"21"),并将Votes设为int。
  • 确保Data类本身是公共的
    如果Data类的访问修饰符是默认的internal,在跨程序集绑定场景下也可能导致异常,必须将类声明为public。


内容的提问来源于stack exchange,提问作者matheus hisatsu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 13:23:11