在我正在编写的WPF应用程序中,我有一个TransformedBitmap属性,它绑定到UI上的Image对象。每当我改变这个属性时,图像被更新(并且因此正在被显示到屏幕上的图像被更新)。为了防止结冰或不响应,而我检索下一个图像的UI,我试图快照检索,像这样一个BackgroundWorker:从BackgroundWorker线程更新图像UI属性
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
e.Result = this.snapshotHelper.GetSnapshot(ImageFormat.Bmp);
}
然后,在我的RunWorkerCompleted方法,我有以下:
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.CurrentImage = (TransformedBitmap)e.Result;
....
}
这似乎是工作好,直到用来告诉Image对象的NotifyPropertyChanged方法来更新,当我更新CurrentImage财产;我得到一个交叉线程错误。
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
//The following causes a "the calling thread cannot access this object because a different thread owns it" error!
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
我真的不知道如何改变事情或做些什么不同来解决这个错误。在过去的几个小时里,我一直在阅读关于BackgroundWorkers的内容,在我看来,我应该能够在RunWorkerCompleted方法中设置好CurrentImage;至少从我能说的来看。任何帮助,将不胜感激!谢谢!
查看我的答案更新。我已经处理了这个问题,它的工作原理。 – 2010-09-13 20:41:56