Xamarin App异常:“引发了Object-C异常。名称:NSInvalidArgumentException”

时间:2019-05-27 19:20:51

标签: c# xaml xamarin xamarin.ios

我尝试构建一个简单的Xamarin Forms应用程序,该应用程序将联系人列表分组。在这种情况下,我只有两个联系人以便于保持联系。在iOS模拟器或iPhone 6s(iOS 12)上启动应用程序时,出现错误提示:

  

“ {Foundation.MonoTouchException:抛出Objective-C异常。名称:   NSInvalidArgumentException原因:***-[NSPlaceholderString   initWithUTF8String:]:NULL cString本机堆栈跟踪:0
  CoreFoundation 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>

1 个答案:

答案 0 :(得分:0)

在此行:

  GroupShortNameBinding="{Binding shortTitle}">

您应将绑定设置为 ShortTitle ,而不是shortTitle

这里是带有代码的演示,您可以检查它:grouped-listView