silverlight中大部分的数据都是通过异步得到的,当得到了数据直接赋给UI的时候,容易出现“跨线程访问无效”错误。

例如:给UI的listbox控件设置数据源:

this.Posts.ItemsSource = blog.Posts;

注意blog.Posts是异步得到的一个数据源,这时就会出现“跨线程访问无效”错误。

解决方法:

if (this.Posts.Dispatcher.CheckAccess())
{
       this.Posts.ItemsSource = blog.Posts;

}
 else
{
       this.Posts.Dispatcher.BeginInvoke(() => { this.Posts.ItemsSource = blog.Posts; });
 }

相关文章:

  • 2022-12-23
  • 2021-09-13
  • 2022-12-23
  • 2021-04-13
  • 2022-03-07
  • 2022-12-23
猜你喜欢
  • 2021-12-19
  • 2022-12-23
  • 2021-06-30
  • 2022-12-23
  • 2021-05-01
  • 2022-12-23
  • 2021-09-09
相关资源
相似解决方案