【问题标题】:Xamarin App Exception: "Objective-C exception thrown. Name: NSInvalidArgumentException"Xamarin 应用程序异常:“引发了 Objective-C 异常。名称:NSInvalidArgumentException”
【发布时间】:2019-05-27 19:20:51
【问题描述】:

我尝试构建一个简单的 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>

【问题讨论】:

    标签: c# xaml xamarin xamarin.ios


    【解决方案1】:

    行内:

      GroupShortNameBinding="{Binding shortTitle}">
    

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

    这是一个带有代码的演示,您可以查看:grouped-listView

    【讨论】:

    • @Dariush 它应该可以工作,我在我这边测试过,我会和你分享这个项目。您可以尝试清理并重建您的项目。
    • @Dariush 更新了我的答案,请检查一下。
    • 非常感谢。我的解决方案找不到问题,但您的解决方案完美。
    猜你喜欢
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多