提问人:Gabic 提问时间:3/20/2023 最后编辑:Gabic 更新时间:3/21/2023 访问量:200
Expander 引发 NullReferenceException
Expander throwing NullReferenceException
问:
我正在开发一个使用 和 的应用程序。在我的一个页面中,我需要显示我们的客户列表,包括他们的姓名和地址,以及 4 个按钮。为了避免冗长的加载过程,我想使用 ,并将名称放在正文中的其余项中。.Net Maui
.Net 7
Community Toolkit Expander
Expander.Header
问题是它在 or 负载之后抛出一个(我尝试了两者,它们都出现了同样的问题)。我的数据不是问题,因为当我只是在 a 中显示所有信息时,不会引发异常。NullReferenceException
ListView
CollectionView
VerticalStackLayout
它曾经适用于我开发的应用程序的先前版本,所以我想知道这是否可能是 中的某种错误,但我没有发现其他人有同样问题的任何信息。Xamarin.Forms
.Net Maui Community Toolkit
这是我的代码:
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#
答:
0赞
Jessie Zhang -MSFT
3/21/2023
#1
根据您的代码,我创建了一个演示并进行了测试,它在我这边工作正常。
您可以参考以下代码:
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 " });
}
}
MainPage.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>
评论
1赞
Gabic
3/21/2023
我创建了一个新项目来单独测试页面,就像你说的,它工作了,但它有点不可预测......我曾经滚动列表太快了,异常又被抛出......我将遵循 Jason 的建议,而不是在 ListView 中使用 Expander
评论
VerticalStackLayout
ScrollView