我正在尝试清除Windows RT App中的列表框项目。要添加项目,我使用:
List<string> list1;
...
foreach(string s in list1.Items)
{
listBox1.Items.Add(s);
}
要清除这些项目,我使用:
listBox1.Items.Clear();
然而,这引发了这个例外:
Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
如果我尝试使用:
int at = 0;
while (at < listBox1.Items.Count)
{
listBox1.Items.RemoveAt(at);
at += 1;
}
我在RemoveAt方法中得到了同样的异常。
答案 0 :(得分:3)
我找到了解决这个问题的方法。我试图从SelectionChanged事件触发的方法中删除项目。我改为:
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, listBox1.Items.Clear);
它运作正常。
答案 1 :(得分:1)
对于第二个,你真的不想增加'at'
如果您在0处删除该项目,则1处的项目将成为0处的项目。
所以
while (listBox1.Items.Count != 0)
{
listBox1.Items.RemoveAt(0);
}
会奏效。
不确定为什么你在第一个例子中得到例外 - 你是否在某个地方启动了列表框?
答案 2 :(得分:0)
如果您的列表使用DataBinding绑定到某些数据,则处理后面的数据模型而不是ListItems。
(如果您正确实施了INotifyPropertyChanged界面,列表将会更新)