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

.NET MAUI中Community Toolkit Expander抛出NullReferenceException问题

问题

我基于.NET 7和.NET MAUI开发应用,需要在页面展示包含名称、地址及4个按钮的客户列表。为优化加载流程,使用Community Toolkit的Expander组件,将客户名称放在Expander.Header区域,其余内容放在主体部分。但使用ListView或CollectionView加载后均抛出NullReferenceException,换成VerticalStackLayout直接展示所有信息则无异常。该功能在之前的Xamarin.Forms版本中可正常运行,怀疑是.NET MAUI Community Toolkit的bug,但未找到同类问题案例。

相关XAML代码

xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
...
<ListView ItemsSource="{Binding SearchResults}"
          HasUnevenRows="True"
          SeparatorVisibility="None">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell x:DataType="m:ProjetoResumo">
                <toolkit:Expander>
                    <toolkit:Expander.Header>
                        <VerticalStackLayout BackgroundColor="{StaticResource WhiteColor}"
                                             Margin="7"
                                             Padding="5">
                            <Label Text="{Binding Nome}"
                                   TextColor="{StaticResource DarkBlueColor}"
                                   FontFamily="MontSemiBold"/>
                            <Image Source="expandarrow.png"
                                   HeightRequest="20"
                                   WidthRequest="20"
                                   HorizontalOptions="End">
                            </Image>
                        </VerticalStackLayout>
                    </toolkit:Expander.Header>
                    <VerticalStackLayout>
                        <Label Text="{Binding Endereco}"
                               FontSize="Caption"/>
                        <Grid ColumnDefinitions="*,*,*,*"
                              HeightRequest="32"
                              Margin="0, 5, 0, 0">
                            <ImageButton Source="info.png"
                                         Grid.Column="0"/>
                            <ImageButton Source="phone.png"
                                         Grid.Column="1"/>
                            <ImageButton Source="whatsapp.png"
                                         Grid.Column="2"/>
                            <ImageButton Source="location.png"
                                         Grid.Column="3"/>
                        </Grid>
                    </VerticalStackLayout>
                </toolkit:Expander>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

异常调用栈

0xFFFFFFFFFFFFFFFF in Android.Runtime.JNIEnv.monodroid_debugger_unhandled_exception C#
0x1A in Android.Runtime.JNINativeWrapper._unhandled_exception at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12,5 C#
0x1D in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:23,26    C#
0xB in CommunityToolkit.Maui.Views.Expander. at /_/src/CommunityToolkit.Maui/Views/Expander/Expander.shared.cs:201,42   C#
0x6 in Microsoft.Maui.Dispatching.Dispatcher. at D:\a\_work\1\s\src\Core\src\Dispatching\Dispatcher.Android.cs:22,24    C#
0xE in Java.Lang.Thread.RunnableImplementor.Run at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Thread.cs:36,6 C#
0x8 in Java.Lang.IRunnableInvoker.n_Run at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net7.0/android-33/mcw/Java.Lang.IRunnable.cs:84,4    C#
0x8 in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:22,5  C#
解决方案
  • 升级Community Toolkit版本:异常指向Expander组件的201行,大概率是旧版本的已知bug。检查NuGet包CommunityToolkit.Maui的版本,升级到最新稳定版(如>=5.3.0),早期版本的Expander在ListView/CollectionView中存在视图初始化或上下文绑定的问题。
  • 调整Expander的包裹结构:在ViewCell中给Expander套一层VerticalStackLayout,避免视图树初始化顺序导致的空引用:
    <ViewCell x:DataType="m:ProjetoResumo">
        <VerticalStackLayout>
            <toolkit:Expander>
                <!-- 原Expander内容不变 -->
            </toolkit:Expander>
        </VerticalStackLayout>
    </ViewCell>
    
  • 添加绑定空值处理:确保ProjetoResumo对象的Nome、Endereco属性不会为null,或在绑定中添加空值 fallback:
    <Label Text="{Binding Nome, FallbackValue=''}" .../>
    <Label Text="{Binding Endereco, FallbackValue=''}" .../>
    
  • 替换为CollectionView并移除ViewCell:CollectionView在MAUI中对复杂视图的兼容性更好,无需ViewCell包裹:
    <CollectionView ItemsSource="{Binding SearchResults}"
                    ItemsLayout="VerticalList">
        <CollectionView.ItemTemplate>
            <DataTemplate x:DataType="m:ProjetoResumo">
                <toolkit:Expander>
                    <!-- 原Expander内容不变 -->
                </toolkit:Expander>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
    
  • 显式设置绑定上下文:在Expander上直接指定绑定上下文,确保Header和主体部分能正确继承DataTemplate的上下文:
    <toolkit:Expander BindingContext="{Binding .}">
        <!-- 原内容 -->
    </toolkit:Expander>
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 00:07:45