【问题标题】:Update in ListView RendererListView 渲染器中的更新
【发布时间】:2017-02-03 08:20:50
【问题描述】:

我需要更新原生 ListView 中的项目数据。

1.使用 Xamarin.Forms ListView。 简单的DataItem 模型与INotifyPropertyChanged

    List<DataItem> Data = new List<DataItem>();
    // ... fill Data
    ListView listDevice = new ListView();
    listDevice.ItemsSource = Data;
    listDevice.ItemTemplate = new DataTemplate(typeof(DataView));
    ...
    void BtnTest_Clicked(object sender, EventArgs e)
    {
        Data[0].DeviceName = "Update OK";
    }

当点击 btnTest 时,我立即在第一个 ListView 元素中看到“更新确定”。一切都很好。

2。使用 ListView 的渲染器。 我使用 WorkingWithListviewNative 示例,它有渲染器和适配器。我将DataSource2 替换为DataItem 模型(无处不在)并添加相同的btnTest。带有数据的列表是可见的,但是当单击按钮时没有任何变化。向下和向上滚动后,我看到了变化。

我需要将哪些代码添加到渲染器(添加到 OnElementPropertyChanged?)或适配器?

【问题讨论】:

    标签: android forms listview xamarin renderer


    【解决方案1】:

    使用 ObservableCollection 代替 List 并且您不需要在处理它时执行属性更改通知。

    【讨论】:

    • ObservableCollection 仅在使用 Add、Remove、Insert 而不是直接编辑时更新 ListView(在第一个示例中)(Data[0].ItemName = "...")。这样,您需要使用脏 hack(保存项目,将其从 DataSource 中删除,编辑已保存的项目,在 DataSource 中插入已保存的项目)无论如何,ObservableCollection 在第二个示例中不起作用(使用自定义渲染器)。我认为必须在渲染器或适配器中添加一些魔术代码。
    • 我假设你可以在自己的类上实现 INotifyCollectionChanged,但我还没有做到。
    猜你喜欢
    • 1970-01-01
    • 2017-12-13
    • 1970-01-01
    • 2017-11-16
    • 1970-01-01
    • 2012-12-22
    • 2011-10-23
    • 1970-01-01
    • 2016-12-30
    相关资源
    最近更新 更多