我有一個問題,與我的WPF應用程序。 我想在我的gridview中用我的viewmodel中的image屬性綁定一個圖像域。異步數據綁定的圖像,導致交叉線程異常
<DataGridTemplateColumn Header="Image" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Path=Image, IsAsync=True}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
這是沒有問題的,如果我不使用IsAsync。 但是,我想做異步,因爲它是很多圖像加載,它需要從web服務加載它們。
Image屬性的代碼是this,它只是調用一個處理程序dll,它調用webservice。
public BitmapSource Image
{
get { return image ?? (image = ImageHandler.GetDefaultImages(new[] {ItemNumber},160,160)[0].BitmapSource()); }
}
但是,只要我添加IsAsync = true時,我得到以下異常,一旦形式加載:
The calling thread cannot access this object because a different thread owns it.
我很新的WPF,和我親切當async設置爲true時,它處理線程本身。我需要以某種方式在數據綁定中調用? 如果是這樣,我該怎麼做?
你可以添加你的圖片屬性的代碼? – mathieu
@mathieu,當然。 – Nicolai
'BitmapSource'源自'Freezable'。如果您在創建位圖後不打算更改位圖,則可以簡單地將其「凍結」。然後任何線程都可以訪問它。 – Niki