Xamarin.Forms Tapped 事件,没有可绑定的属性

问题描述

我正在尝试获取viewmodel 填充的 ListView。参考 StackOverflow 文章,这应该是一个可行的解决方案。这篇文章可以在这里找到ListView not triggering ItemTapped

尝试编译时出现以下错误

No property,BindableProperty,or event found for "ItemTapped",or mismatching type between value and property.

如果我从 xaml 文件删除 ItemTapped,项目将编译并填充列表。最终目标是使 ListView 中的每个项目都可点击,从而指向特定的站。

下面是我的代码片段

StationSelectorPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Company.Forms.Views.StationSelectorPage"
            xmlns:vm="clr-namespace:Company.Forms.viewmodels"
             Title="{Binding Title}">
    <ContentPage.BindingContext>
        <vm:StationSelectorviewmodel />
    </ContentPage.BindingContext>

    <ContentPage.Content>
        <StackLayout>
            
            <Label Text="Select Station:" />
            
            <ListView 
                x:Name="StationView" 
                ItemsSource="{Binding Stations}"
                ItemTapped="{Binding OnItemTapped}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextCell Text="{Binding StationName}" />
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

StationSelectorPage.xaml.cs

    [XamlCompilation(XamlCompilationoptions.Compile)]
    public partial class StationSelectorPage : ContentPage
    {
        
        public StationSelectorPage()
        {
            InitializeComponent();
        }
    }

StationSelectorviewmodel.cs


public class StationSelectorviewmodel : Baseviewmodel
{
    public List<StationsModel> m_Stations;
    public List<AreasModel> m_Areas;
    public ApiConnection m_apiConnection;
    ObservableCollection<StationsModel> stations = new ObservableCollection<StationsModel>();
    public ObservableCollection<StationsModel> Stations { get { return stations; }}


    public StationSelectorviewmodel()
    {
        Title = "Station Selector";
        m_apiConnection = new ApiConnection(Constants.Url,null,null);
        GetStations();
    }

    public async void GetStations()
    {
        string absolutePath = DependencyService.Get<IMisc>().GetAbsolutePath();

        m_Areas = await m_apiConnection.HttpGetAsync<List<AreasModel>>("misc/get-areas");

        m_Stations = 
            m_Areas.
            Select(x => new StationsModel { StationId = x.StationId,StationName = x.StationName })
            .distinct()
            .ToList();

        foreach(var station in m_Stations)
        {
            Stations.Add(new StationsModel { StationId = station.StationId,StationName = station.StationName });
        }

    }


    public async void OnItemTapped(object group,object item,int itemIndex)
    {
        var selectedItem = item;
    }
}

解决方法

ItemTapped 处理程序的签名是

{{1}}