[英]Xamarin Forms ListView: Access to ViewCell's Object data
I'm new to Xamarin and I'm facing a problem using a ListView to list posts, like Facebook. 我是Xamarin的新手,使用ListView列出帖子(例如Facebook)时遇到了问题。
I've made a class Post and it works as it should, but now I want to interact with posts (example: like, comment) and for that I have to access each ListView Item values to process it in the database. 我制作了一个Post类,它可以正常工作,但是现在我想与Post交互(例如:like,comment),为此,我必须访问每个ListView Item值以在数据库中对其进行处理。
How can I acess the item's data? 我如何访问商品的数据?
Feed.xaml.cs Feed.xaml.cs
ObservableCollections<Post> posts;
//Binding data to ListView
postsList.ItemSource = posts;
posts.Add(new Post{ Key = "key_example", Title = "title_example", Img = "http://example.example"});
///////if i do the following, it gives me a null pointer exception\\\\\\\\
this.LoadFromXaml(typeof(Feed));
Image image = this.FindByName<Image>("key_image")
Feed.xaml Feed.xaml
<ListView x:Name="postsList">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Label Text = { Binding Title } />
<Image x:Name = "key_image"/>
<Button /> //CALLS FUNCTION IN .cs WITH key VALUE FROM CURRENT POST
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
this will bind the Button's CommandParameter property to the relevant Post object 这会将Button的CommandParameter属性绑定到相关的Post对象
<Button CommandParameter="{Binding .}" Text="Blah" />
then in your click handler or command you can reference the CommandParameter and cast it to Post. 然后在点击处理程序或命令中,您可以引用CommandParameter并将其强制转换为Post。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.