我已经和这个例外(以及随后的硬崩溃)争夺了大约一天.我在Xamarin Forms中有一个ListView,可以对标题进行分组.尽管我可以从调用堆栈中收集,但由于分组,错误正在发生.我通过从ListView中删除分组来验证这一点,并且应用程序显示数据而不会崩溃.我甚至将代码差异化了几周,并没有什么突出的.这也仅在新的启动时发生,后续启动不会抛出此异常或崩溃.
我还有什么可以进一步调试和解决这个问题?
列表视图XAML
<ListView x:Name="ListViewItems" ItemsSource="{Binding ItemsGrouped}" GroupdisplayBinding="{Binding Key.displayText}" IsGroupingEnabled="true"> <ListView.GroupHeaderTemplate> <DataTemplate> <ViewCell> <cells:MyGroupHeaderView/> </ViewCell> </DataTemplate> </ListView.GroupHeaderTemplate> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <cells:ItemCellView /> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView>
MyGroupHeaderView XAML
<StackLayout VerticalOptions="Center"> <Label Text="{Binding Key.displayText}"> </Label> </StackLayout>
分组C#代码
var result = from s in myItems orderby s.ItemDateTime group s by s.GetSortGroupInfo() into itemGroup select new Grouping<GroupInfo,Itemviewmodel>(itemGroup.Key,itemGroup);
GroupInfo类(用于排序和显示)
public class GroupInfo { public string displayText { get; set; } public int SortValue { get; set; } }
绑定属性和Setter
public ObservableRangeCollection<Grouping<GroupInfo,Itemviewmodel>> ItemsGrouped { get; } = new ObservableRangeCollection<Grouping<GroupInfo,Stopviewmodel>>(); this.ItemsGrouped.ReplaceRange( this.MyItems.GroupByDate() .OrderBy(f => f.Key.sortValue) .ToList());
解决方法
根本问题是
TemplatedItemsList没有很好地处理ReplaceRange生成的
Reset.
作为短期解决方法,您可以通过不使用ReplaceRange来避免崩溃:
ItemsGrouped.Clear(); foreach (var item in MyItems.GroupByDate().OrderBy(f => f.Key.sortValue)) { ItemsGrouped.Add(item); }
或者将caching strategy设置为回收:
<ListView x:Name="ListViewItems" ItemsSource="{Binding ItemsGrouped}" IsGroupingEnabled="true" CachingStrategy="RecycleElement">
有趣的是,如果你按照调用链来到UnhookItem,它有这样的评论:
//Hack: the cell Could still be visible on iOS because the cells are reloaded after this unhook //this causes some visual updates caused by a null datacontext and default values like IsVisible if (Device.RuntimePlatform == Device.iOS && CachingStrategy == ListViewCachingStrategy.RetainElement) await Task.Delay(100); item.BindingContext = null;
对我的崩溃看起来是由于尝试重新渲染模板,因为绑定上下文已经改变…所以也许这个hack也适用于Android …
您正在上面的示例代码中设置GroupdisplayBinding和GroupHeaderTemplate …我假设您正在尝试…您应该一次只有一个…使用GroupdisplayBinding也可以避免我期望的问题.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。