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

时间:2018-08-15 07:28:35

标签: c# xaml xamarin xamarin.forms xamarin.android

我正在尝试让应用程序在加载时执行方法。在我的.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命名空间或其他内容?如何解决该错误?

1 个答案:

答案 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();
            });

        });

    }

这应该可以实现您的目标。