dictionary - .NET MAUI Permissions RequestAsync not waiting for user to respond like Xamarin did - Stack Overflow

In Xamarin forms, if you call Permissions.RequestAsync<Permissions.LocationWhenInUse>, it will wa

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
Add a comment  | 

1 Answer 1

Reset to default 0

To make sure your app runs smoothly when requesting permissions, here are a few helpful tips:

  1. 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.

  2. 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信