最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin5浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论