c#


Xamarin.Forms Map doesn't get along with async await


When I try to add a pin to the map, the app halts. It doesn't continue executing the next piece of code. This happens only when I call DoWork from await; however not calling await doesn't not halt the app.
By halt, I mean it doesn't crash the application it just doesn't execute the next line of code yet I can still interact with the application.
Am I missing something or using async wrong?
Map myMap;
public MyViewModel(Map map)
{
myMap = map;
// Causes crash
Task.Run(async () =>
{
await DoWork();
});
// Causes no crashes
//DoWork();
}
async Task<bool> DoWork()
{
var success = false;
Task<bool> task = null;
task = SomeTask();
if (await task)
{
var pin = MyPin();
myMap.Pins.Add(pin);
// This is never called when calling await Dowork(). Application stops.
myMap.MoveToRegion(MapSpan.FromCenterAndRadius(MyPosition(), Distance.FromMiles(5)));
success = true;
}
return success;
}
public Pin MyPin()
{
var pin = new Pin();
pin.Type = PinType.Place;
pin.Position = new Position(<latitude>, <longitude>);
return pin;
}
The problem here is that you need to execute MoveToRegion from the UI-Thread.
if running the task with Task.Run(() => ... you are invoking it on a background thread.
to make sure the ui manipulation is done on the mainthread you can force it
Device.BeginInvokeOnMainThread(() =>
{
var pin = MyPin();
myMap.Pins.Add(pin);
// This is never called when calling await Dowork(). Application stops.
myMap.MoveToRegion(MapSpan.FromCenterAndRadius(MyPosition(), Distance.FromMiles(5)));
success = true;
});

Related Links

send selected item from listbox to database table
Error converting String in to DateTime format
WPF - How to change StackPanel/ItemControl at runtime?
Replacing string literals in c# using Regex [closed]
Unit test is passing when it should fail
Determining source of timeout on client
Access route information from authentication filter in ASP.NET API
how to get battery status of a bluetooth headphone in c#
What is the RSA Algorithm used by C# by default, and what is it's appropriate parameter in Crypto++?
ASP MVC 5 - 403 customError not working
Display DataGridView from multiple sources in ACCESS
Possible combination from dynamic data sets
Replace text inside any <p> tag html with C#
WPF Custom DataTemplateSelector based on Enum property in Custom UserControl
Lag in PDF annotation with pen
How to avoid WinForm freezing using Task.Wait

Categories

HOME
asterisk
qlikview
twitter-bootstrap
kendo-ui-angular2
microcontroller
spotfire
symfony-sonata
zabbix
sitemap
sahi
normals
http-status-codes
mongodb-replica-set
element
github-flavored-markdown
web-testing
jsfiddle
airbnb
loader
turn.js
concatenation
uipath
functional-dependencies
suitescript
rancher
datanucleus
bytebuffer
jbossfuse
ocl
foxpro
akamai
language-detection
choco
storefront
microstrategy
outlook-2010
word-wrap
julia-jump
delphi-xe
compiled-query
glide-image-library
directadmin
contenteditable
amazon-fire-tv
anonymous-function
yank
ionic2-select
hellosign
nls
toggleclass
easy68k
hotkeys
kik
boost-bind
video-embedding
fadein
clear
consul-template
eaaccessory
genome
shunting-yard
frisby.js
mind-manager
gnip
windows-search
xpand
yandex-api
traceur
autofilter
maven-javadoc-plugin
kohana-orm
angularjs-select2
microsoft-speech-platform
libx264
volatility
custom-cursor
parsec
eeprom
mass-emails
fxcopcmd
nyromodal
for-xml
hp-trim
file-icons
vista64
memory-consumption

Resources

Encrypt Message