处理文本文件的数据虚拟化

本文关键字:数据 虚拟化 文件 文本 处理 | 更新日期: 2023-09-27 18:27:21

我阅读了这个链接:

http://www.codeproject.com/Articles/34405/WPF-Data-Virtualization

使用数据虚拟化读取文本文件:

在DemoCustomerProvider.cs中,我更改了:

 for( int i=startIndex; i<startIndex+count; i++ )
        {
            Customer customer = new Customer { Id = i + 1, Name = "Customer" + (i+1) };
            list.Add(customer);
        }

至:

  for( int i=startIndex; i<startIndex+count; i++ )
        {
            using (StreamReader str = new StreamReader("C:''test.txt"))
            {
                while (str.ReadLine() != null)
                {
                    string data=str.ReadLine();
                    Customer customer = new Customer { Id = i + 1, Name =data }   
                    list.Add(customer);
                }
            }
           }

文本大小为2 mb,但当启动数据虚拟化时,它会使用3 GB的内存!

我想知道如何使用数据虚拟化来读取文本文件?

处理文本文件的数据虚拟化

由于您正在读取整个文本文件,因此您违背了数据虚拟化的目的,即只延迟加载数据的可见部分。为了实现这一点,您需要一个具有固定记录大小的数据库或文件,并且可以从中读取与UI中显示的部分相对应的数据子集。请仔细重读这篇文章,以了解它需要什么。此外,如果您只处理价值2MB的数据,请避免不必要的复杂性。不要用大锤打碎螺母。

作为对您评论的跟进,让我为您提供以下提示:您正在重复读取文件,并获取count×文件中的记录数。每次滚动时都会执行代码。从那里开始,并开始相应地修改代码。