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
You probably don't want to call Task.Result
, for a couple of reasons.
Firstly, as I explain in detail on my blog, you can deadlock unless your async
code is has been written using ConfigureAwait
everywhere. Secondly, you probably don't want to (synchronously) block your UI; it would be better to temporarily show a "loading..." or blank image while reading from the disk, and update when the read completes.
So, personally, I would make this part of my ViewModel, not a value converter. I have a blog post describing some databinding-friendly ways to do asynchronous initialization. That would be my first choice. It just doesn't feel right to have a value converter kicking off asynchronous background operations.
However, if you've considered your design and really think an asynchronous value converter is what you need, then you have to get a bit inventive. The problem with value converters is that they have to be synchronous: the data binding starts at the data context, evaluates the path, and then invokes a value conversion. Only the data context and path support change notifications.
So, you have to use a (synchronous) value converter in your data context to convert your original value into a databinding-friendly Task
-like object and then your property binding just uses one of the properties on the Task
-like object to get the result.
Here's an example of what I mean:
<TextBox Text="" Name="Input"/>
<TextBlock DataContext="{Binding ElementName=Input, Path=Text, Converter={local:MyAsyncValueConverter}}"
Text="{Binding Path=Result}"/>
The TextBox
is just an input box. The TextBlock
first sets its own DataContext
to the TextBox
's input text running it through an "asynchronous" converter. TextBlock.Text
is set to the Result
of that converter.
The converter is pretty simple:
public class MyAsyncValueConverter : MarkupExtension, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var val = (string)value;
var task = Task.Run(async () =>
{
await Task.Delay(5000);
return val + " done!";
});
return new TaskCompletionNotifier<string>(task);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
The converter first starts an asynchronous operation to wait 5 seconds and then add " done!" to the end of the input string. The result of the converter can't be just a plain Task
because Task
doesn't implement IPropertyNotifyChanged
, so I'm using a type that will be in the next release of my AsyncEx library. It looks something like this (simplified for this example; full source is available):
// Watches a task and raises property-changed notifications when the task completes.
public sealed class TaskCompletionNotifier<TResult> : INotifyPropertyChanged
{
public TaskCompletionNotifier(Task<TResult> task)
{
Task = task;
if (!task.IsCompleted)
{
var scheduler = (SynchronizationContext.Current == null) ? TaskScheduler.Current : TaskScheduler.FromCurrentSynchronizationContext();
task.ContinueWith(t =>
{
var propertyChanged = PropertyChanged;
if (propertyChanged != null)
{
propertyChanged(this, new PropertyChangedEventArgs("IsCompleted"));
if (t.IsCanceled)
{
propertyChanged(this, new PropertyChangedEventArgs("IsCanceled"));
}
else if (t.IsFaulted)
{
propertyChanged(this, new PropertyChangedEventArgs("IsFaulted"));
propertyChanged(this, new PropertyChangedEventArgs("ErrorMessage"));
}
else
{
propertyChanged(this, new PropertyChangedEventArgs("IsSuccessfullyCompleted"));
propertyChanged(this, new PropertyChangedEventArgs("Result"));
}
}
},
CancellationToken.None,
TaskContinuationOptions.ExecuteSynchronously,
scheduler);
}
}
// Gets the task being watched. This property never changes and is never <c>null</c>.
public Task<TResult> Task { get; private set; }
Task ITaskCompletionNotifier.Task
{
get { return Task; }
}
// Gets the result of the task. Returns the default value of TResult if the task has not completed successfully.
public TResult Result { get { return (Task.Status == TaskStatus.RanToCompletion) ? Task.Result : default(TResult); } }
// Gets whether the task has completed.
public bool IsCompleted { get { return Task.IsCompleted; } }
// Gets whether the task has completed successfully.
public bool IsSuccessfullyCompleted { get { return Task.Status == TaskStatus.RanToCompletion; } }
// Gets whether the task has been canceled.
public bool IsCanceled { get { return Task.IsCanceled; } }
// Gets whether the task has faulted.
public bool IsFaulted { get { return Task.IsFaulted; } }
// Gets the error message for the original faulting exception for the task. Returns <c>null</c> if the task is not faulted.
public string ErrorMessage { get { return (InnerException == null) ? null : InnerException.Message; } }
public event PropertyChangedEventHandler PropertyChanged;
}
By putting these pieces together, we've created an asynchronous data context that is the result of a value converter. The databinding-friendly Task
wrapper will just use the default result (usually null
or 0
) until the Task
completes. So the wrapper's Result
is quite different than Task.Result
: it won't synchronously block and there's no danger of deadlock.
But to reiterate: I'd choose to put asynchronous logic into the ViewModel rather than a value converter.
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<Xamarin.Forms.ImageSource>), typeof(AsyncSourceSvgCachedImage), null, propertyChanged: SourceAsyncPropertyChanged);
public Task<Xamarin.Forms.ImageSource> AsyncSource
{
get { return (Task<Xamarin.Forms.ImageSource>)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<Xamarin.Forms.ImageSource>;
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.
Below Code is perfect working to me.
Example.xaml
<Window xmlns:converters = "namespace your converter"
<Grid>
<Grid.Resources>
<converters:ExampleConverterWithAsync x:Name = "TestConverter"/>
</Grid.Resources>
<TextBlock DataContext="{Binding YourNeedsItem, Converter={StaticResource TestConverter}}" Text="{Binding AsyncValue}"/>
</Grid>
</Window>
ExampleConverterWithAsync.cs
public class ExampleConverterWithAsync : IValueConverter
{
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return new TaskNotifier<string>(Task.run(async() =>
{
Task.Delay(1_000); // replace your job
return "Hello World";
}));
}
public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Binding.DoNothing;
public class TaskNotifier<T> : ViewModelBase
where T : class
{
private T _asyncValue;
public T AsyncValue
{
get => _asyncValue;
set { this.SetValue(ref _asyncValue, value); }
}
public TaskNotifier(Task<T> valueFunc)
{
Task.Run(async () =>
{
AsyncValue = await valueFunc;
});
}
}
}