MainPage.OnStart()找不到适合的方法来重写

时间:2018-08-26 22:30:58

标签: c# xamarin xamarin.forms

我试图在MainPage.cs中运行以下内容:

public class MainPage : ContentPage
{
    //...

    protected override async void OnStart()
    {
        var request = new GeolocationRequest(GeolocationAccuracy.Best);
        var location = await Geolocation.GetLocationAsync(request);
        Position position = new Position(location.Latitude, location.Longitude);
        Geocoder geocoder = new Geocoder();
        var f = await geocoder.GetAddressesForPositionAsync(position);

        Debug.WriteLine(string.Join(",", f));
        Debug.WriteLine("exeuctie");
    }
}

但是,我收到了错误MainPage.OnStart() No Suitable Method Found to Override

在添加OnStart之前,我的应用程序运行良好。

1 个答案:

答案 0 :(得分:1)

ContentPage没有OnStart方法;只有ApplicationOnStart

您可以在OnAppearing上使用ContentPage。每当屏幕上出现ContentPage时,就会触发以下代码:

protected override async void OnAppearing()
{
    var request = new GeolocationRequest(GeolocationAccuracy.Best);
    var location = await Geolocation.GetLocationAsync(request);
    Position position = new Position(location.Latitude, location.Longitude);
    Geocoder geocoder = new Geocoder();
    var f = await geocoder.GetAddressesForPositionAsync(position);

    Debug.WriteLine(string.Join(",", f));
    Debug.WriteLine("exeuctie");
}