【发布时间】:2018-07-07 14:08:11
【问题描述】:
直截了当。 我的列表视图看起来像下面的 xaml(不是 100% 相同,但应该足够详细)
<ListView ItemsSource="{Binding Result}"
SelectedItem="{Binding Selected}"
HasUnevenRows="True">My content...</ListView>
这绑定到我的视图模型,看起来像这样
private CancellationTokenSource cts = null
private CancellationToken ct = CancellationToken.None
private List<Task> tasks = new List<Task>()
public ObservableCollection<T> Source = new ObservableCollection<T>()
public ObservableCollection<T> Result = new ObservableCollection<T>()
public string Query
它也有以下方法。这种方法是我的问题所在。该方法的行为应该是这样的:
- 从源获取所有元素(无论从哪里来)
- 启动一个将在后台运行的任务(逐步构建列表,减少同时生成的列表视图项的数量,性能)
- 因此它将逐步将元素添加到结果中(在我的情况下,最多同时添加 100 个元素)
这是代码 注意:源已经成功加载,元素在里面
cts.Cancel();
cts.Dispose();
Task.WaitAll(tasks.ToArray());
tasks.Clear();
cts = new CancellationTokenSource();
ct = cts.Token;
Result.Clear();
// Create background task
tasks.Add(Task.Factory.StartNew(() =>
{
var validItems = Source.Where(c => ((ISearchQueryViewModel)c).SearchData.RegexContains(Query)).OrderByDescending(c => ((ISearchQueryViewModel)c).Aktive).ToList();
if (ct.IsCancellationRequested)
return;
var numberOfValidItemsToAdd = validItems.Count;
var currentIndex = 0;
while (numberOfValidItemsToAdd > 0)
{
if (ct.IsCancellationRequested)
return;
var numberOfIndexes = 100;
if (numberOfValidItemsToAdd < 100)
numberOfIndexes = numberOfValidItemsToAdd;
for (int i = 0; i < numberOfIndexes; i++)
{
currentIndex++;
if (ct.IsCancellationRequested)
return;
Result.Add(validItems[i]);
}
numberOfValidItemsToAdd -= numberOfIndexes;
for (int i = 0; i < 100; i++)
{
if (ct.IsCancellationRequested)
return;
Thread.Sleep(50);
}
}
}, ct));
这导致错误
未处理的异常:
Foundation.MonoTouchException:抛出 Objective-C 异常。姓名: NSInternalInconsistencyException 原因:无效更新:无效 第 0 节中的行数。包含在第 0 节中的行数 更新后的现有节(1)必须等于 更新 (1) 之前该部分中包含的行,加号或减号 从该部分插入或删除的行数(插入 1 行, 0 已删除)加或减移入或移出的行数 该部分(0 移入,0 移出)。本机堆栈跟踪:0
核心基础 0x00000001104da12b __exceptionPreprocess + 171 1 libobjc.A.dylib 0x000000011dda6f41 objc_exception_throw + 48 2 CoreFoundation
0x00000001104df2f2 +[NSException raise:format:arguments:] + 98 3
基础 0x0000000111109d69 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193 4
UIKit 0x0000000114652209-[UITableView _endCellAnimationsWithContext:] + 19416 5 UIKit 0x000000011466d075 -[UITableView endUpdates] + 75 6
MobileClient.iOS 0x000000010fdfd9f9 xamarin_dyn_objc_msgSend + 217 7 ???
0x000000013f12d452 0x0 + 5353165906
【问题讨论】:
-
我认为您遇到了错误bugzilla.xamarin.com/show_bug.cgi?id=59896 - 尽管错误的标题非常具体,但似乎更多的场景会触发它,阅读 cmets。
-
@Depechie 谢谢,它解决了错误。 (小解决方法)我将其发布为答案,希望您对此感到满意 c:非常感谢,祝您有美好的一天 c:
-
@Depechie well^^ 经过几次测试,同样的错误来了^^ 如果你知道一个好的解决方法会很棒 c: 谢谢你,祝你有美好的一天 c:
标签: c# ios objective-c listview xamarin.forms