我有一个绑定到ObservableCollection的ListView,我想要做的是在View中获取一些用户输入并将一个Object添加到我的Collection中,以便我的ListView得到通知。
不幸的是,当我使用以下异常消息向我的Collection中添加内容时,我的应用程序崩溃:调用目标引发了异常。这非常令人沮丧我试过一切都不知道为什么我的应用程序因简单的Collection Add而崩溃。 / p>
ListView
<ListView x:Name="lstFruits"
ItemsSource="{Binding FruitModel.FruitOptionModel.CurrentFruit.FruitCollection}"
ItemTemplate="{StaticResource TemplateSelector}"
SeparatorVisibility="None"
Grid.Row="1" >
<ListView.Behaviors>
<commands:EventToCommandBehavior EventName="ItemTapped" Command="{Binding TappedCommand}" EventArgsConverter="{StaticResource ItemTappedConverter}" />
</ListView.Behaviors>
</ListView>
并使用以下代码添加项目:
FruitModel.FruitOptionModel.CurrentFruit.FruitCollection.Add(FruitObject);
<ContentPage.Resources>
<ResourceDictionary>
<commands:ItemTappedEventArgsConverter x:Key="ItemTappedConverter"/>
<local:GeräteListItemTemplateSelector x:Name="GeräteListItemTemplateSelector" x:Key="TemplateSelector" />
</ResourceDictionary>
</ContentPage.Resources>
public GeräteListItemTemplateSelector()
{
neuesGerätDataTemplate = new DataTemplate(typeof(NeuesGerätViewCell));
gerätDataTemplate = new DataTemplate(typeof(GerätViewCell));
}
private readonly DataTemplate neuesGerätDataTemplate;
private readonly DataTemplate gerätDataTemplate;
protected override DataTemplate OnSelectTemplate(object item, BindableObject container) {
var gerät = item as GerätModel;
if (gerät.NeuesGerät) {
return neuesGerätDataTemplate;
}
else {
return gerätDataTemplate;
}
}
堆栈跟踪:
at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00016] in /Users/builder/data/lanes/3412/3cf8aaed/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:650
at System.RuntimeType.CreateInstanceMono (Boolean nonPublic) [0x000ca] in /Users/builder/data/lanes/3412/3cf8aaed/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/ReferenceSources/RuntimeType.cs:115
at System.RuntimeType.CreateInstanceSlow (Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, System.Threading.StackCrawlMark& stackMark) [0x0001a] in /Users/builder/data/lanes/3412/3cf8aaed/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/ReferenceSources/RuntimeType.cs:89
at System.RuntimeType.CreateInstanceDefaultCtor (Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, System.Threading.StackCrawlMark& stackMark) [0x0002a] in /Users/builder/data/lanes/3412/3cf8aaed/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/rttype.cs:5599
at System.Activator.CreateInstance (System.Type type, Boolean nonPublic) [0x00040] in /Users/builder/data/lanes/3412/3cf8aaed/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/activator.cs:201
at System.Activator.CreateInstance (System.Type type) [0x00000] in /Users/builder/data/lanes/3412/3cf8aaed/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/activator.cs:134
at Xamarin.Forms.ElementTemplate+<>c__DisplayClass3_0.<.ctor>b__0 () [0x00000] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\ElementTemplate.cs:23
at Xamarin.Forms.ElementTemplate.CreateContent () [0x00026] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\ElementTemplate.cs:78
at Xamarin.Forms.DataTemplateExtensions.CreateContent (Xamarin.Forms.DataTemplate self, System.Object item, Xamarin.Forms.BindableObject container) [0x00014] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\DataTemplateExtensions.cs:12
at Xamarin.Forms.TemplatedItemsList`2[TView,TItem].CreateContent (Int32 index, System.Object item, Boolean insert) [0x00000] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\TemplatedItemsList.cs:527
at Xamarin.Forms.TemplatedItemsList`2[TView,TItem].ConvertContent (Int32 startingIndex, IList items, Boolean forceCreate, Boolean setIndex) [0x00014] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\TemplatedItemsList.cs:664
at Xamarin.Forms.TemplatedItemsList`2[TView,TItem].OnProxyCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e, Boolean fixWindows) [0x0017e] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\TemplatedItemsList.cs:1002
at Xamarin.Forms.TemplatedItemsList`2[TView,TItem].OnProxyCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00000] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\TemplatedItemsList.cs:954
at Xamarin.Forms.ListProxy.OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x0000a] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\ListProxy.cs:230
at Xamarin.Forms.ListProxy+<>c__DisplayClass32_0.<OnCollectionChanged>b__0 () [0x00018] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\ListProxy.cs:203
at Xamarin.Forms.ListProxy.OnCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x000a0] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\ListProxy.cs:222
at Xamarin.Forms.ListProxy+WeakNotifyProxy.OnCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00031] in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Core\ListProxy.cs:386
at System.Collections.ObjectModel.ObservableCollection`1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00012] in /Users/builder/data/lanes/3412/3cf8aaed/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/System/compmod/system/collections/objectmodel/observablecollection.cs:288
at System.Collections.ObjectModel.ObservableCollection`1[T].OnCollectionChanged (NotifyCollectionChangedAction action, System.Object item, Int32 index) [0x00000] in /Users/builder/data/lanes/3412/3cf8aaed/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/System/compmod/system/collections/objectmodel/observablecollection.cs:351
at System.Collections.ObjectModel.ObservableCollection`1[T].InsertItem (Int32 index, System.Collections.ObjectModel.T item) [0x00024] in /Users/builder/data/lanes/3412/3cf8aaed/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/System/compmod/system/collections/objectmodel/observablecollection.cs:219
at System.Collections.ObjectModel.Collection`1[T].Add (System.Collections.ObjectModel.T item) [0x00023] in /Users/builder/data/lanes/3412/3cf8aaed/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/collections/objectmodel/collection.cs:67
at LindeXF.ViewModels.FruitViewModel+<<get_NextButtonCommand>b__85_0>d.MoveNext () [0x00118] in C:\Users\valon\Desktop\Linde\LindeXF\LindeXamarinForms\LindeXF\LindeXF\LindeXF\ViewModels\FruitViewModel.cs:386