微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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}}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。