ObservableCollection缓存和绑定

时间:2016-05-28 01:03:20

标签: c# binding uwp observablecollection

此代码填充了ListView绑定的ObservableCollection,它完美地运行。它涉及查询多对多数据库实现。我现在正试图通过缓存已经加载的列表来提高性能。这是现在的代码:

IEnumerable<IEnumerable<string>>

这是绑定属性:

public void List_for_Display(int _id)
    {            
        IEnumerable<Central> items;//central is the many-to-many db implementation
        items = conn.Query<Central>("select * from Central where _id = ?", _id.ToString());
        Cat_V.Clear();//this is the property to which the ListView is bound
        foreach (Central in items)
        {
            var top1 = (from q in conn.Table<Topic>()//Topic is of the tables tahat is linked
                        where q.id == d._id
                        select q).FirstOrDefault();
            Cat_V.Add(top1);
        }
    }

要缓存已加载的ObservableCollections,我将数组声明为变量:

  private ObservableCollection<Topic> cat_V = new ObservableCollection<Topic>();
    public ObservableCollection<Topic> Cat_V
    {
        get { return cat_V; }
        set
        {
            cat_V = value;
            this.OnPropertyChanged();
        }
    }

将保存初始化为适当的大小,并使用Public ObservableCollection<Topic> [] Saving ; 初始化每个元素。信息正确存储在数组中的ObservableCollections中。这是代码:

Saving[_index] = new ObservableCollection<Topic>();

上述功能首次正确加载ui,但第一次分配后没有。引发了OnPropertyChanged,但与第一个版本不同,ui未更新。

谢谢!

更新

ListView绑定:public void List_for_Display(int _cat_ind, int _cat_id) { if (Saving[_cat_ind] == null) { Saving[_cat_ind] = new ObservableCollection<Topic>(); IEnumerable<Central> items; items= conn.Query<Central>("select * from Central where cat_id = ?", _cat_id.ToString()); foreach (CenTab1 d in items) { var top1 = (from q in conn.Table<Topic>() where q.id == d.top_id select q).FirstOrDefault(); Saving[_cat_ind].Add(top1); } } Cat_V = Saving[_cat_ind]; }

OnPropertyChanged方法:

ItemsSource="{x:Bind ViewModel.Cat_V}"

0 个答案:

没有答案