3

I have a converter in my Windows Phone application but it seems you cannot use "await" methods in it?

public object Convert(object value, Type targetType, object parameter, string language)
{
    _IDataService = ServiceLocator.Current.GetInstance<IDataService>();

    string imageUrlId = (string)value;

    byte[] imageByte = await iDataService.GetImage(imageUrlId);

    return LoadImageAsync(imageByte);
}

If I make the method async Task it says IValueConverter has no method Task async. This converter returns type ImageSource.

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
Bayern
  • 330
  • 3
  • 20

3 Answers3

4

You can't change the signature of the Convert method since you have to provide an implementation for all the methods of the IValueConverter interface.

What I usually do is loading in the images of the view-model asynchronously from the backend, not on the UI thread. With the proper bindings, the images will be displayed on your UI as soon as it is loaded.

Kzryzstof
  • 7,688
  • 10
  • 61
  • 108
  • I see, and can I use await async in one of my model classes? In the get from a property? – Bayern Dec 12 '15 at 14:28
  • No, you can't use the async keyword on a property. (See http://stackoverflow.com/questions/12384309/async-property-in-c-sharp-4-5) Just make your GetImage method of your IDataService interface asynchronous. Loads the image from your view-model by calling the async GetImage and assign the BitmapImage to one of the property of the view-model. :) – Kzryzstof Dec 12 '15 at 14:32
  • Yes I got it! But I need to go to the detail page and then back to see the images, when I login I see my ListView with empty images, going to a detail page and back they are shown. – Bayern Dec 12 '15 at 20:51
  • I'm glad you got it :) – Kzryzstof Dec 13 '15 at 13:55
1

I have an older answer here that mostly addresses this problem (but see below - one of the helper types is somewhat dated).

The core of the problem is that asynchronous work is actually improper for a value converter to do. What you're really doing is telling WPF "to cast this type to this other type, first call a web service..."

So, IMO any asynchronous behavior is better done in other parts of the system (ViewModel, service layer, etc). Asynchronous work can be easily represented in a ViewModel by using my NotifyTask type.

However, my older answer does have a working "asynchronous value converter". Note that the TaskCompletionNotifier type in that answer has a newer implementation.

Community
  • 1
  • 1
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • Now I need to know how to update the xaml UI after a background task is done. – Bayern Dec 12 '15 at 20:59
  • @Bayern: That's what the `NotifyTask` type is for. I have an [MSDN article](https://msdn.microsoft.com/en-us/magazine/dn605875.aspx) with example usage. – Stephen Cleary Dec 12 '15 at 21:49
  • Ahaha that looks interesting! That will work for this problem? http://stackoverflow.com/questions/34244838/images-downloaded-in-a-async-method-shown-after-coming-back-from-a-detail-page – Bayern Dec 12 '15 at 22:01
  • @Bayern: From what I can tell of the question, it looks like yes. `NotifyTask` is specifically for updating MVVM UIs with the results of background operations. – Stephen Cleary Dec 13 '15 at 01:47
  • OK then I will try this. I also found out that when I scroll down and back up the images are also shown. – Bayern Dec 13 '15 at 09:30
-2

You could use .result () after async function call

Like, byte[] imageByte = iDataService.GetImage(imageUrlId).result ();

ITGenius
  • 129
  • 2
  • 14