Xamarin.Forms Tapped 事件,没有可绑定的属性
Posted
技术标签:
【中文标题】Xamarin.Forms Tapped 事件,没有可绑定的属性【英文标题】:Xamarin.Forms Tapped event, no bindable property 【发布时间】:2021-03-31 18:16:09 【问题描述】:我正在尝试获取从 ViewModel 填充的 ListView。参考 *** 文章,这应该是一个可行的解决方案。这篇文章可以在这里找到ListView not triggering ItemTapped。
尝试编译时出现以下错误:
No property, BindableProperty, or event found for "ItemTapped", or mismatching type between value and property.
如果我从 xaml 文件中删除 ItemTapped,项目将编译并填充列表。最终目标是使 ListView 中的每个项目都可点击,从而指向特定的站点。
下面是我的代码的sn-ps
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;
【问题讨论】:
【参考方案1】:ItemTapped 处理程序的签名是
void ItemTappedHandler(object sender, Xamarin.Forms.ItemTappedEventArgs args)
【讨论】:
我将函数调整为:public async void OnItemTapped(object sender, Xamarin.Forms.ItemTappedEventArgs args) System.Console.WriteLine("Testing");
这给我带来了与No property, BindableProperty, or event found for "ItemTapped", or mismatching type between value and property.
相同的错误
啊,我没听懂。我不认为你绑定事件处理程序。
这种方式不能绑定 OnItemTapped 事件。你可以用Xamarin Community Toolkit ItemTappedEventArgsConverter这个video also explains怎么用好。以上是关于Xamarin.Forms Tapped 事件,没有可绑定的属性的主要内容,如果未能解决你的问题,请参考以下文章