我尝试构建一个简单的Xamarin Forms应用程序,该应用程序对联系人列表进行分组。 在这种情况下,我只有两个联系人来保持简单。在iOS模拟器或我的iPhone 6s(iOS 12(上启动应用程序时,出现错误,指出:
"{Foundation.MonoTouchException: Objective-C exception thrown. 名字: NSInvalidArgumentException Reason: *** -[NSPlaceholderString initWithUTF8String:]: NULL cString Native stack trace: 0
核心基金会0x00000001068e56fb __exceptionPrepr...}">
C#
namespace Lists{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
listView.ItemsSource = new List<ContactGroup> {
new ContactGroup("P", "P")
{
new Contact { Name = "Peter Parker", Status = "Nice to meet you!" }
},
new ContactGroup("J", "J")
{
new Contact { Name = "John Smith", Status = "Hey, let's talk!" }
}
};
}
}
public class Contact
{
public string Name { get; set; }
public string Status { get; set; }
}
public class ContactGroup : List<Contact>
{
public string Title { get; set; }
public string ShortTitle { get; set; }
public ContactGroup(string title, string shortTitle)
{
Title = title;
ShortTitle = shortTitle;
}
}
}
XAML
<?xml version="1.0" encoding="utf-8"?>
<ContentPage
Padding="0, 20, 0, 0"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Lists" x:Class="Lists.MainPage">
<ListView x:Name="listView"
IsGroupingEnabled="true"
GroupDisplayBinding="{Binding Title}"
GroupShortNameBinding="{Binding shortTitle}">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Name}" Detail="{Binding Status}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
在行中:
GroupShortNameBinding="{Binding shortTitle}">
应将绑定设置为">短标题"而不是"shortTitle
"。
这是一个带有代码的演示,您可以检查它:grouped-listView