Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@JakeGinnivan
Created March 22, 2013 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JakeGinnivan/5219390 to your computer and use it in GitHub Desktop.
Save JakeGinnivan/5219390 to your computer and use it in GitHub Desktop.
[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