XAML Expander抛出NullReferenceException

oug3syen  于 2023-03-27  发布在  其他
关注(0)|答案(1)|浏览(122)

我正在使用.Net Maui.Net 7开发一个应用程序。在我的一个页面中,我需要显示我们的客户的名称和地址的列表,以及4个按钮。为了避免长时间的加载过程,我想使用Community Toolkit Expander,并将名称放在Expander.Header中,其余项目放在正文中。
问题是它在ListViewCollectionView加载后抛出了NullReferenceException(我试过这两个,它们出现了同样的问题)。这不是我的数据的问题,因为当我只是显示VerticalStackLayout中的所有信息时,没有抛出异常。
它曾经在我使用Xamarin.Forms开发的应用程序的以前版本上工作,所以我想知道这是否可能是.Net Maui Community Toolkit中的某种bug,但我没有发现任何其他人有同样的问题。
下面是我的代码:

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#
ffdz8vbo

ffdz8vbo1#

基于您的代码,我创建了一个演示并进行了测试,它在我这边工作正常。
您可以参考以下代码:

ProjetoResumo.cs

public class ProjetoResumo 
{
    public string Nome { get; set; }
    public string Endereco { get; set;}
}

我的视图模型.cs

public class MyViewModel 
{
    public ObservableCollection<ProjetoResumo> Items { get; set; } 

    public MyViewModel() {
        Items = new ObservableCollection<ProjetoResumo>();
        Items.Add( new ProjetoResumo { Nome = "test1", Endereco = "end1 " });
        Items.Add(new ProjetoResumo { Nome = "test2", Endereco = "end2 " });
        Items.Add(new ProjetoResumo { Nome = "test3", Endereco = "end3 " });

    }
}

主页.xaml

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             xmlns:local="clr-namespace:MauiApp320.Models"
             xmlns:viewmodels="clr-namespace:MauiApp320.ViewModels"
             x:Class="MauiApp320.MainPage">

    <ContentPage.BindingContext>
        <viewmodels:MyViewModel></viewmodels:MyViewModel>
    </ContentPage.BindingContext>

    <ListView ItemsSource="{Binding Items}"
          HasUnevenRows="True"
          SeparatorVisibility="None">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell x:DataType="local:ProjetoResumo">
                        <toolkit:Expander>
                            <toolkit:Expander.Header>
                                <VerticalStackLayout 
                                             Margin="7"
                                             Padding="5">
                                    <Label Text="{Binding Nome}"
                                     FontFamily="MontSemiBold"/>
                                    <Image Source="arrow.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="image1.png"
                                         Grid.Column="0"/>
                                    <ImageButton Source="image2.png"
                                         Grid.Column="1"/>
                                    <ImageButton Source="image3.png"
                                         Grid.Column="2"/>
                                    <ImageButton Source="image4.png"
                                         Grid.Column="3"/>
                                </Grid>
                            </VerticalStackLayout>
                        </toolkit:Expander>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

</ContentPage>

相关问题