When running a .Net MAUI app using the Windows simulator, I have the following code (stripped down to include the relevant statements):
task = Task.Run( async () => await do_something( cancellation_token ), cancellation_token );
The do_something() method calls Geolocation.Default.GetLocationAsync, as shown below:
do_something( CancellationToken ct )
{
try
{
location = await Geolocation.Default.GetLocationAsync( geo_location_request, ct );
}
catch ( Exception )
{
/*
At this point, an exception is raised:
Microsoft.Maui.ApplicationModel.PermissionException: Permission request must be invoked on main thread
at Microsoft.Maui.ApplicationModel.Permissions.LocationWhenInUse.RequestLocationPermissionAsync()
*/
}
}
As per the comments above, an exception is raised that states the call to Geolocation.Default.GetLocationAsync() must be invoked on the main thread.
However, when using the Android simulator, no exception is raised and everything works fine. I did not see anything mentioned in the documentation that the call to Geolocation.Default.GetLocationAsync() must be invoked on the main thread.
Apologies if such information does exist and I missed it -- if that is the case, please provide the relevant link.
Questions
- Is this an undocumented "feature"?
- Is this a bug in the Windows simulator?
I can get around the issue (by invoking the task on the MainThread), but I want to know if it is really necessary to do so given that it works on Android, but not on Windows.
Thanks in advance.