我正在尝试让应用程序在加载时执行方法。在我的.xaml中, `
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="LoginApp.Droid.Views.LocationPage"
NavigationPage.HasNavigationBar="False"
BackgroundColor="#E5FFC4"
Loaded="LocationPage_Loaded">
这是在我的.xaml.cs文件中:
public partial class LocationPage : ContentPage
{
public LocationPage ()
{
InitializeComponent ();
}
private async void LocationPage_Loaded(object sender, EventArgs e)
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
var position = await locator.GetPositionAsync(TimeSpan.FromMinutes(3), includeHeading: true);
Entry_Longitude.Text = position.Longitude.ToString();
Entry_Latitude.Text = position.Latitude.ToString();
}
根据VS的错误与.xaml中的行Loaded="LocationPage_Loaded">
有关
我是否缺少xmlns命名空间或其他内容?如何解决该错误?
答案 0 :(得分:1)
没有这样的Event
称为“为ContentPage
加载”。但是,根据您的情况,您可以覆盖OnAppearing()
LoactionPage.xaml.cs
protected override void OnAppearing()
{
base.OnAppearing();
Task.Run(async () =>
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
var position = await locator.GetPositionAsync(TimeSpan.FromMinutes(3), includeHeading: true);
Device.BeginInvokeOnMainThread(() =>
{
Entry_Longitude.Text = position.Longitude.ToString();
Entry_Latitude.Text = position.Latitude.ToString();
});
});
}
这应该可以实现您的目标。