I have an asynchronous method which I want to trigger inside an IValueConverter
.
Is there a better way than forcing it to be synchronous by calling the
The alternative approach is to make your own control which supports async sources or data.
Here is the example with the image
public class AsyncSourceCachedImage : CachedImage
{
public static BindableProperty AsyncSourceProperty = BindableProperty.Create(nameof(AsyncSource), typeof(Task), typeof(AsyncSourceSvgCachedImage), null, propertyChanged: SourceAsyncPropertyChanged);
public Task AsyncSource
{
get { return (Task)GetValue(AsyncSourceProperty); }
set { SetValue(AsyncSourceProperty, value); }
}
private static async void SourceAsyncPropertyChanged(BindableObject bindable, object oldColor, object newColor)
{
var view = bindable as AsyncSourceCachedImage;
var taskForImageSource = newColor as Task;
if (taskForImageSource != null)
{
var awaitedImageSource = await taskForImageSource;
view.Source = awaitedImageSource;
}
}
}
Moreover, you might implement the loading activity indicator over the image until the task will be resolved.