简体   繁体   中英

Xamarin.Forms Tapped event, no bindable property

I am attempting to get a ListView that is populated from the ViewModel. Referencing StackOverflow article this should be a working solution. The article can be found here ListView not triggering ItemTapped .

When attempting to compile I get the following error:

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

If I remove the ItemTapped from the xaml file the project will compile and the list will populate. The end goal is to make each item in the ListView clickable that leads to the specific station.

Below is the snippets of my code

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;
    }
}

the signature of the ItemTapped handler is

void ItemTappedHandler(object sender, Xamarin.Forms.ItemTappedEventArgs args)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM