0

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?

8
  • freeze for a moment till the is loaded. Commented Apr 15, 2014 at 5:29
  • 2
    Unfortunately the Binding.IsAsync property only affects the property getter, but not the binding converter. So you may consider to replace the converter by an additional view model property like CurrentImage which you can bind to asynchronously without converter. Commented Apr 15, 2014 at 6:34
  • @Clemens Any samples? I'm not familiar with MVVM. Thanks Commented Apr 15, 2014 at 6:58
  • 1
    Just add another property to the class where CurrentItem is defined. Commented Apr 15, 2014 at 7:03
  • @Clemens I used IsSynchronizedWithCurrentItem so the CurrentItem is not defined by myself. Commented Apr 15, 2014 at 7:14

2 Answers 2

1

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}"/>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Although I met some thread problems, it is the best solution anyway.
@SeanC. how did you fix your thread problems? my images never show up
@Julien Like he said, do the trick in the getter. The converter will not work asynchronously. If that does not work for you, check if your image source has been triggered.
0

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

1 Comment

It seems like this solution is to change the source property in converter. The source property is already bound. Will this twist the binding setting?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.