You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取Xamarin.Forms ListView选中项的id值并赋值给NewId

嘿,我来帮你搞定这个问题!要实现选中ListView项时把对应id赋值给BlogClass的NewId字段,咱们只需要几步简单的修改,具体如下:

第一步:修正XAML中的ListView并添加选中事件

首先你XAML里的ListView名称是BloglistView,但C#代码里用的是BlogListView,名称不统一会导致控件找不到,咱们先把名称改一致,同时给ListView添加ItemSelected事件来捕获选中操作:

<?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="demoListView.ImageCellPage">
<ContentPage.Content>
<!-- 统一名称为BlogListView,并绑定ItemSelected事件 -->
<ListView x:Name="BlogListView" ItemSelected="OnBlogItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout BackgroundColor="#eee" Orientation="Vertical">
<StackLayout Orientation="Horizontal">
<Image Source="{Binding BlogImageSource}" />
<Label Text="{Binding id}" TextColor="#f35e20" />
<Label Text="{Binding DisplayTopic}" HorizontalOptions="EndAndExpand" TextColor="#503026" />
<Label Text="{Binding DisplayMain}" HorizontalOptions="EndAndExpand" TextColor="#503026" />
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
</ContentPage>

第二步:调整BlogClass的代码

原来的BlogClass里直接给页面控件BlogListView赋值是不对的(类里不应该直接访问页面控件),咱们把BlogList1改成可访问的属性,移除直接设置ItemsSource的代码:

public class BlogClass
{
    public string NewId;
    // 把BlogList1改成公开属性,让页面可以访问
    public List<BlogViews> BlogList1 { get; private set; } = new List<BlogViews>();

    public BlogClass()
    {
        AddItem();
    }

    public class BlogViews
    {
        public string id { get; set; }
        public string DisplayTopic { get; set; }
        public string DisplayMain { get; set; }
        public ImageSource BlogImageSource { get; set; }
    }

    public void AddItem()
    {
        BlogList1.Add(new BlogViews { id = "1", DisplayMain = "Margret", DisplayTopic = "Mensah" });
        BlogList1.Add(new BlogViews { id = "2", DisplayMain = "Maet", DisplayTopic = "Meah" });
        BlogList1.Add(new BlogViews { id = "3", DisplayMain = "dargret", DisplayTopic = "sah" });
        BlogList1.Add(new BlogViews { id = "4", DisplayMain = "gret", DisplayTopic = "Meh" });
        // 移除这行:BlogListView.ItemsSource = BlogList1;
    }
}

第三步:在页面代码中实现选中事件逻辑

打开你的ImageCellPage.xaml.cs(对应XAML里的x:Class),添加事件处理方法,同时在构造函数里设置ListView的数据源:

public partial class ImageCellPage : ContentPage
{
    // 保存BlogClass的实例,方便后续访问
    private BlogClass _blogInstance;

    public ImageCellPage()
    {
        InitializeComponent();
        _blogInstance = new BlogClass();
        // 在这里给ListView设置数据源
        BlogListView.ItemsSource = _blogInstance.BlogList1;
    }

    // 处理ListView的选中事件
    private void OnBlogItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        // 先判断选中项是否有效,避免空引用
        if (e.SelectedItem is BlogClass.BlogViews selectedBlogItem)
        {
            // 把选中项的id赋值给BlogClass的NewId字段
            _blogInstance.NewId = selectedBlogItem.id;
            
            // 可选操作:取消选中状态,这样用户再次点击同一项时能重新触发事件
            ((ListView)sender).SelectedItem = null;
        }
    }
}

关键说明

  • 一定要保证ListView的名称在XAML和C#中完全一致,否则会出现控件找不到的错误;
  • 通过ItemSelected事件可以准确捕获用户的选中操作,事件参数里的SelectedItem就是我们绑定的BlogViews对象;
  • 要持有BlogClass的实例引用,这样才能给它的NewId字段赋值;
  • 取消选中状态是个可选但实用的操作,避免用户重复点击同一项时无法触发事件。

内容的提问来源于stack exchange,提问作者LBG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:17:25