如何在 MainPage 中调用异步方法

how to call async method in MainPage

public partial class MainPage : ContentPage
{    
    public  MainPage()
    {
        InitializeComponent();
    }
}

我想打电话给这里:

var status = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();

怎么做?

OnAppearing

中调用
public override async void OnAppearing()
{
    var status = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();
}

如果你不想在 OnAppearing() 方法中调用它,特别是在构造函数中,你可以这样做(不过要注意可能的线程阻塞):

public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();

            // watch out for possible thread blocking!
            Task.Run(async() =>
            {
                await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();
            });
        }
    }