In Xamarin forms, if you call Permissions.RequestAsync<Permissions.LocationWhenInUse>, it will wait for the user to respond before proceeding in the code. In .NET MAUI, the code doesn't wait. As an async method, this is probably by design. Is there a way to get .NET MAUI to work like Xamarin did? I've tried running the code as a task, and tried forcing the code to run on the main thread. Nothing seems to work. Our goal is to prompt the user for permissions only when they get to the map page of our app, the first time. Then, depending on what permissions they grant, proceed to their current location, or the default location.
if ((PermissionStatus)await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>() != PermissionStatus.Granted)
{
if ((PermissionStatus)await Permissions.RequestAsync<Permissions.LocationWhenInUse>() == PermissionStatus.Granted)
{
Task<Location> task = Task.Run(() => ViewModel.GetCurrentLocation());
ViewModel.CurrentLocation.Latitude = task.Result.Latitude;
ViewModel.CurrentLocation.Longitude = task.Result.Longitude;
}
else
{
// Default location
ViewModel.CurrentLocation.Latitude = [Default latitude];
ViewModel.CurrentLocation.Longitude = [Default longitude];
}
}
else
{
Task<Location> task = Task.Run(() => ViewModel.GetCurrentLocation());
ViewModel.CurrentLocation.Latitude = task.Result.Latitude;
ViewModel.CurrentLocation.Longitude = task.Result.Longitude;
}
// Move map to either user's location or default location
LodgeMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(ViewModel.CurrentLocation.Latitude, ViewModel.CurrentLocation.Longitude), Distance.FromMiles(40)));
In Xamarin forms, if you call Permissions.RequestAsync<Permissions.LocationWhenInUse>, it will wait for the user to respond before proceeding in the code. In .NET MAUI, the code doesn't wait. As an async method, this is probably by design. Is there a way to get .NET MAUI to work like Xamarin did? I've tried running the code as a task, and tried forcing the code to run on the main thread. Nothing seems to work. Our goal is to prompt the user for permissions only when they get to the map page of our app, the first time. Then, depending on what permissions they grant, proceed to their current location, or the default location.
if ((PermissionStatus)await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>() != PermissionStatus.Granted)
{
if ((PermissionStatus)await Permissions.RequestAsync<Permissions.LocationWhenInUse>() == PermissionStatus.Granted)
{
Task<Location> task = Task.Run(() => ViewModel.GetCurrentLocation());
ViewModel.CurrentLocation.Latitude = task.Result.Latitude;
ViewModel.CurrentLocation.Longitude = task.Result.Longitude;
}
else
{
// Default location
ViewModel.CurrentLocation.Latitude = [Default latitude];
ViewModel.CurrentLocation.Longitude = [Default longitude];
}
}
else
{
Task<Location> task = Task.Run(() => ViewModel.GetCurrentLocation());
ViewModel.CurrentLocation.Latitude = task.Result.Latitude;
ViewModel.CurrentLocation.Longitude = task.Result.Longitude;
}
// Move map to either user's location or default location
LodgeMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(ViewModel.CurrentLocation.Latitude, ViewModel.CurrentLocation.Longitude), Distance.FromMiles(40)));
Share
Improve this question
asked Mar 24 at 14:32
user2521509user2521509
11 silver badge1 bronze badge
1 Answer
Reset to default 0To make sure your app runs smoothly when requesting permissions, here are a few helpful tips:
Await the Permission Request: Always wait for the permission request to finish before handling the result. It's best to avoid using
Task.Run()
for this, as it can complicate things unnecessarily.Use Async/Await: Instead of freezing the UI or trying to run tasks in a blocking way, embrace the async/await pattern throughout your method. This will keep your app responsive and user-friendly.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744246724a4564968.html
评论列表(0条)