Listbox.Items.Clear()在SelectionChanged上崩溃

时间:2011-08-29 13:12:55

标签: c# silverlight listbox

private void lbcreatedRequests_SelectionChanged(object sender, SelectionChangedEventArgs e)
{

}

//Display Edit Page
private void btnEdit_Click(object sender, RoutedEventArgs e)
{
   lbcreatedRequests.Items.Clear(); //crash here 
   _loggedIn = Home._loggedIn;
    if (_loggedIn == true)
    {
       wcf.retrievecreatedrequestListAsync(Home.myID);
    }
}

在再次加载新版本之前,尝试清除Listbox

错误

  

对象引用未设置为对象的实例。

     

at SGTaxiPool.Views.ShareCab.lbcreatedRequests_SelectionChanged(Object sender,SelectionChangedEventArgs e)     在System.Windows.Controls.Primitives.Selector.OnSelectionChanged>(SelectionChangedEventArgs e)     在System.Windows.Controls.Primitives.Selector.InvokeSelectionChanged(List 1 unselectedItems, List 1 selectedItems)     在System.Windows.Controls.Primitives.Selector.SelectionChanger.End()     在System.Windows.Controls.Primitives.Selector.OnItemsChanged(NotifyCollectionChangedEventArgs e)     在System.Windows.Controls.ListBox.OnItemsChanged(NotifyCollectionChangedEventArgs e)     在System.Windows.Controls.ItemsControl.OnItemCollectionChanged(Object sender,NotifyCollectionChangedEventArgs e)     at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object> sender,NotifyCollectionChangedEventArgs e)     在System.Windows.Controls.ItemCollection.NotifyCollectionChanged>(NotifyCollectionChangedEventArgs e)     在System.Windows.Controls.ItemCollection.ClearImpl()     在System.Windows.PresentationFrameworkCollection`1.Clear()     at SGTaxiPool.Views.ShareCab.btnEdit_Click(Object sender,RoutedEventArgs e)     在System.Windows.Controls.Primitives.ButtonBase.OnClick()     在System.Windows.Controls.Button.OnClick()     在System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)     在System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl,EventArgs e)     在MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj,IntPtr unmanagedObjArgs,Int32 argsTypeIndex,Int32 actualArgsTypeIndex,String eventName

1 个答案:

答案 0 :(得分:1)

This thread建议此问题的解决方法是在Dispatcher线程上执行此操作:

private void lbcreatedRequests_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   Dispatcher.BeginInvoke(() => { lbcreatedRequests.Items.Clear(); });
}