Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[TestMethod]
public async Task TestLocationSearchHasResult()
{
var testVm = new GeocodeViewModel { SearchText = "Springerstraat Amersfoort Netherlands" };
await Deployment.Current.Dispatcher.InvokeTaskAsync(() => testVm.SearchLocation());
Assert.IsTrue(testVm.MapLocations.Any());
}
public static class DispatcherExtensions
{
public static Task<T> InvokeTaskAsync<T>(this Dispatcher dispatcher, Func<Task<T>> func)
{
var tcs = new TaskCompletionSource<T>();
dispatcher.BeginInvoke(new Action(async () =>{
try
{
var result = await func();
tcs.SetResult(result);
}
catch (Exception e)
{
tcs.SetException(e);
}
}));
return tcs.Task;
}
public static Task<T> InvokeAsync<T>(this Dispatcher dispatcher, Func<T> func)
{
var tcs = new TaskCompletionSource<T>();
dispatcher.BeginInvoke(new Action(()=>{
try
{
var result = func();
tcs.SetResult(result);
}
catch (Exception e)
{
tcs.SetException(e);
}
}));
return tcs.Task;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.