简体   繁体   English

Xamarin Forms ListView:访问ViewCell的对象数据

[英]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.

相关问题 如何通过索引从 xamarin 形式的 ListView 对象获取访问视单元? - How to get access viewcell by index from ListView object in xamarin forms? Xamarin.Forms中如何获取ListView的ViewCell数据并发送到另一个页面 - How to get ListView's ViewCell data and send it to another page in Xamarin.Forms Xamarin Forms - ListView 中的 iOS 动态 ViewCell 大小 - Xamarin Forms - iOS dynamic ViewCell size in a ListView 创建列表后,如何更新Xamarin Forms ListView的ViewCell属性? - How can I update a Xamarin Forms ListView's ViewCell properties after the list has been created? 如何在ListView Xamarin Forms中创建舍入的第一个和最后一个视单元? - How to create a rounded first and last viewcell in ListView Xamarin Forms? 如何从 Xamarin Forms 中的自定义 ViewCell 获取 ListView 项目索引? - How to get ListView item index from custom ViewCell in Xamarin Forms? Xamarin.Forms MVVM TapGestureRecognizer 到 ListView 的 ViewCell 中的标签(在部分文件中) - Xamarin.Forms MVVM TapGestureRecognizer to a Label in a ViewCell of ListView (in partial files) 如何自定义要在Xamarin Forms ListItem的ViewCell中显示的数据? - How can I customize the data to display in Xamarin Forms ListItem's ViewCell? Xamarin表单访问列表视图 - Xamarin Forms Access Listview 如何在Xamarin Forms中点击ViewCell后刷新TableView数据? - How to refresh TableView data after tapping ViewCell in Xamarin Forms?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM