Вопрос

I have a Image control with binding settings like this:

<Image Stretch="Uniform" Source="{Binding Path=CurrentItem, Converter={StaticResource ImgConverter}, IsAsync=True}"/>

And the ImgConverter is:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string uri = null;

            Compressor.Unzip((value as Zipfile));

            uri = string.Format("{0}{1}.jpg", Compressor.TempPath, value.ToString());

            return uri;

        }

The Compressor.Unzip(...) method does take some time. I set the binding IsAsync=True but it doesn't work(NOT on converter but only on path?). How can I handle this asynchronously?

Это было полезно?

Решение

Add a readonly Image property to your Zipfile class, and move the converter code to the property getter:

public class Zipfile
{
    ...

    public ImageSource Image
    {
        get
        {
            Compressor.Unzip(this);
            var uri = string.Format("{0}{1}.jpg", Compressor.TempPath, this.ToString());
            return new BitmapImage(new Uri(uri));
        }
    }
}

Then write your binding like this:

<Image Source="{Binding Path=CurrentItem.Image, IsAsync=True}"/>

Другие советы

Using Multibinding with two binding, First is self image control and second is your current binding, in converter you can call async your method to unzip the file and in callback you can set image path(refer to first binding); Also you can see this sample:

http://social.msdn.microsoft.com/Forums/en-US/7fc238ea-194e-4f29-bcbd-9a3d4bdb2180/async-loading-of-bitmapsource-in-value-converter?forum=wpf

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top