在我正在編寫的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