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

Xamarin Forms中NFC标签数据在ListView展示的实现问题

NFC数据传递到ListView展示解决方案

问题核心

已完成NFC数据读取(能获取证件号cedula和学生姓名estudiante),但无法将数据传递至另一个类的ListView中展示。当前代码尝试将数据存入JSON字符串并通过静态类EstudianteData传递,但在数据接收与ListView绑定环节存在问题。

代码修正与实现步骤

1. 确保数据传递通道可靠

先检查静态类EstudianteData的定义,保证它能正确存储JSON数据:

public static class EstudianteData
{
    // 定义公开静态属性用于存储JSON字符串
    public static string listado { get; set; } = string.Empty;
}

同时确认NFC读取完成后,正确调用GuardarDatosJson方法传入参数:

// NFC读取到数据后执行
string cedula = data.Replace("en", string.Empty);
string estudiante = data2.Replace("en", string.Empty);
// 传入姓名和证件号
GuardarDatosJson(estudiante, cedula);

2. 修复数据存储稳定性问题

当前代码中JObject o是类的实例变量,若类实例被销毁,之前存储的数据会丢失,建议改为静态变量:

// 将JObject改为静态,确保数据在程序生命周期内持续保存
private static JObject o = new JObject();

public void GuardarDatosJson(String nombre, String cedula)
{
    JArray array = new JArray();
    array.Add(nombre);
    array.Add(DateTime.Now.ToString());
    
    // 用证件号+时间戳作为唯一键存储数据
    o[cedula + DateTime.Now.Ticks] = array;
    
    // 更新静态类中的JSON字符串
    EstudianteData.listado = o.ToString();
}

3. 在ListView所在类中解析数据并绑定

在ListView所属的窗体/类中,编写解析JSON并绑定到ListView的方法:

// 定义实体类封装学生数据
public class EstudianteItem
{
    public string Nombre { get; set; }
    public string FechaLectura { get; set; }
    public string Identificador { get; set; }
}

// 刷新ListView的方法
private void ActualizarListView()
{
    if (string.IsNullOrEmpty(EstudianteData.listado)) return;
    
    // 解析JSON字符串
    JObject jsonData = JObject.Parse(EstudianteData.listado);
    List<EstudianteItem> estudianteList = new List<EstudianteItem>();
    
    foreach (var kvp in jsonData)
    {
        JArray dataArray = kvp.Value as JArray;
        if (dataArray != null && dataArray.Count >= 2)
        {
            estudianteList.Add(new EstudianteItem
            {
                Nombre = dataArray[0].ToString(),
                FechaLectura = dataArray[1].ToString(),
                Identificador = kvp.Key
            });
        }
    }
    
    // WinForms示例:绑定到ListView
    listViewEstudiantes.Items.Clear();
    foreach (var item in estudianteList)
    {
        ListViewItem lvItem = new ListViewItem(item.Nombre);
        lvItem.SubItems.Add(item.FechaLectura);
        lvItem.SubItems.Add(item.Identificador);
        listViewEstudiantes.Items.Add(lvItem);
    }
}

4. 触发ListView自动刷新

为了在NFC读取完成后自动刷新ListView,可在NFC读取类中定义静态事件:

// 在NFC读取类中定义事件
public static event Action OnDatosActualizados;

// 调用GuardarDatosJson后触发事件
public void GuardarDatosJson(String nombre, String cedula)
{
    // 原有的存储逻辑
    o[cedula + DateTime.Now.Ticks] = array;
    EstudianteData.listado = o.ToString();
    
    // 触发刷新事件
    OnDatosActualizados?.Invoke();
}

然后在ListView所在类的构造函数中订阅该事件:

public TuListViewForm()
{
    InitializeComponent();
    // 订阅事件,自动触发刷新
    NFCReaderClase.OnDatosActualizados += ActualizarListView;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 21:25:30