为 ListView 设置 ItemSource 在 iOS 上崩溃,在 Android 中工作正常



我正在使用 Xamarin 窗体创建一个应用程序,其中HomePage在主StackLayout内部有一个ListView。 分组在我的列表中启用,并在我的 XAML 中声明,如下所示:

<ListView x:Name="listsListView" IsGroupingEnabled="True" GroupDisplayBinding="{Binding groupName}"  
        HasUnevenRows="True" GroupShortNameBinding="{Binding ShortName}">
  <ListView.GroupHeaderTemplate>
      <DataTemplate>
          <ViewCell>
              <Label Text="{Binding groupName}"/>
          </ViewCell>
      </DataTemplate>
  </ListView.GroupHeaderTemplate>
  <ListView.ItemTemplate>
      <DataTemplate>
          <ViewCell>
              <Label Text="{Binding itemName}"/>
          </ViewCell>
      </DataTemplate>
  </ListView.ItemTemplate>

在我的代码隐藏中,我设置了ListView的内容:

//ListGroup extends List<Item> and gives the name for the group header
group1 = new ListGroup("Group 1");
group2 = new ListGroup("Group 2");
//I then add a few items to my ListGroups
groupedLists = new List<ListGroup>();
groupedLists.Add(childListGroup); 
groupedLists.Add(listsListGroup); 
listsListView.ItemsSource = groupedLists;

这是我对ListGroup的实现:

public class ListGroup : List<Item>
{
    public string groupName { get; set; }
    public string ShortName { get; set; }
    public ListGroup(string Name)
    {
        this.groupName = Name;
        ShortName = "";
    }
}

我进行了代码调试,在设置ListView ItemSource时,错误发生在最后一行。 奇怪的是,一切都在Android中运行良好。 但是,在iOS中,我得到:">*由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'* -[NSPlaceholderString initWithUTF8String:]:NULL cString'">

有谁知道为什么我在iOS上收到此错误? 我已经做Android一段时间了,但对iOS很陌生。

Date/Time:       2017-03-23T01:58:34Z
Launch Time:     2017-03-23T01:55:34Z
OS Version:      Mac OS X 10.2 (16D32)
Report Version:  104
Exception Type:  SIGABRT
Exception Codes: #0 at 0x11a944dd6
Crashed Thread:  0
Application Specific Information:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSPlaceholderString initWithUTF8String:]: NULL cString'
Last Exception Backtrace:
0   CoreFoundation                       0x0000000118af9d33 0x118a03000 + 1010995
1   libobjc.A.dylib                      0x0000000119a4421e 0x119a3f000 + 21022
2   CoreFoundation                       0x0000000118b632b5 0x118a03000 + 1442485
3   Foundation                           0x00000001105a2624 0x11058c000 + 91684
4   MyApp.iOS                         0x000000010dcd29a0 0x10d9f1000 + 3021216
5   MyApp.iOS                         0x000000010dcdc73a 0x10d9f1000 + 3061562
6   MyApp.iOS                         0x000000010dcd568a 0x10d9f1000 + 3032714
7   MyApp.iOS                         0x000000010dcdbdbd 0x10d9f1000 + 3059133
8   MyApp.iOS                         0x000000010dcdd171 0x10d9f1000 + 3064177
9   UIKit                                0x000000010eacff71 0x10e96e000 + 1449841
10  MyApp.iOS                         0x000000010dcdd2f9 0x10d9f1000 + 3064569
11  ???                                  0x00000001321d76ae 0x0 + 0

我不确定为什么,但是当我从 XAML 中删除GroupShortNameBinding="{Binding ShortName}"行时,它现在可以工作了。 我想我一直在遵循教程,并在意外中将其留在我的代码中。

最新更新