图像的异步数据绑定会导致跨线程异常

Nic*_*lai 3 data-binding wpf asynchronous datagridview bitmapsource

我有一个问题,我的WPF应用程序.我正在尝试使用viewmodel中的image属性在我的gridview中对图像字段进行数据绑定.

<DataGridTemplateColumn Header="Image" IsReadOnly="True">
    <DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <Image Source="{Binding Path=Image, IsAsync=True}" />
    </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Run Code Online (Sandbox Code Playgroud)

如果我不使用IsAsync,这没问题.但是,我想做异步,因为它需要加载大量图像,并且需要从Web服务加载它们.

Image属性的代码是this,它只调用一个调用web服务的处理程序dll.

    public BitmapSource Image
    {
        get { return image ?? (image = ImageHandler.GetDefaultImages(new[] {ItemNumber},160,160)[0].BitmapSource()); }
    }
Run Code Online (Sandbox Code Playgroud)

但是,只要我添加了IsAsync = true,我就会在表单加载后得到以下异常:

The calling thread cannot access this object because a different thread owns it.

我是WPF的新手,我有点假设,当async设置为true时,它处理了线程本身.在数据绑定中是否需要以某种方式调用?如果是这样,我该怎么做呢?

Jul*_*ain 6

IsAsync意味着将从另一个线程访问绑定属性,因此请确保您创建的任何内容都可以以跨线程方式使用.绑定的最后一步将始终发生在主线程上.

几乎WPF中的所有类都继承自来DispatcherObject,它基本上将对象"链接"到当前线程创建时.在BitmapSource你的代码是在另一个线程创建的,然后不能在主UI线程上使用.

但是,BitmapSource继承自Freezable:在返回之前简单地调用Freeze你的方法BitmapSource,使其在任何线程中都是不可变的和可用的.