我试图在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之前,我的应用程序运行良好。
答案 0 :(得分:1)
ContentPage
没有OnStart
方法;只有Application
有OnStart
。
您可以在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");
}