未找到“已加载”的属性、可绑定属性或事件,或值与属性之间的类型不匹配

我正在尝试让应用程序在加载时执行方法。在我的 .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 的错误与Loaded="LocationPage_Loaded">.xaml 中的行有关我是否缺少 xmlns 命名空间或其他什么?如何解决错误?


摇曳的蔷薇
浏览 224回答 1
1回答

千巷猫影

没有Event所谓的 Loaded for ContentPage。然而,在你的情况,你可以覆盖OnAppearing()在LoactionPage.xaml.cs&nbsp; &nbsp; protected override void OnAppearing()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; base.OnAppearing();&nbsp; &nbsp; &nbsp; &nbsp; Task.Run(async () =>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var locator = CrossGeolocator.Current;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; locator.DesiredAccuracy = 50;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var position = await locator.GetPositionAsync(TimeSpan.FromMinutes(3), includeHeading: true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Device.BeginInvokeOnMainThread(() =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Entry_Longitude.Text = position.Longitude.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Entry_Latitude.Text = position.Latitude.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }这应该可以实现您的目标。
打开App,查看更多内容
随时随地看视频慕课网APP