我試圖更新從另一個線程中的事件我的用戶界面中的圖像。我正在使用Dispatcher(基於這個問題:Fire events from different thread)這樣做,但仍然得到「調用線程無法訪問此對象,因爲不同的線程擁有它」 - 錯誤消息i.Source = s;
。這樣做的正確方法是什麼?通過調度程序更新映像仍然給「不同的線程擁有它」錯誤
void OnEvent(object sender, EventArgs e)
{
ImageSource s = e.Image;
Dispatcher.BeginInvoke((Action)(
() => UpdateUI(myImage, s)
));
}
void UpdateUI(Image i, ImageSource s)
{
i.Source = s;
}
非常感謝您的任何建議!
您正在調度當前線程,而不是UI線程。您首先需要訪問UI線程調度程序。你從什麼樣的對象派遣? – BatteryBackupUnit