问题
I have an app which gets content from internet. those pieces of information are so large and can't fit in memory.
LongListSelector
does UI Virtualization
well. remains Data Virtualization
. I thought the solution is to save data in database first then show it.
I have no idea how to do it and these are questions in my head:
- Is that how I should do
Data Virtualization
? - what happens if there is no enough space.
- any source or tip is appreciated.
thanks.
回答1:
Basic idea of data virtualization is creating custom collection that can load & return item(s) on-demand (without prior loading complete set in memory). Following is radically simplified implementation (adapted from this blog post) :
namespace VirtualizingDataTest
{
public class VirtualizedDataSource : IList
{
public object this[int index]
{
get
{
string text = "Requesting\t" + index;
Debug.WriteLine(text);
return "Item " + index;
}
set
{
throw new NotImplementedException();
}
}
}
In the sample above, new item created upon requested. In your case, if the online source provide a way to request item in specific index, you don't need database. You can put logic to download specific item in this[]
getter. Further references (various better/more complete implementation) can be found here : https://stackoverflow.com/a/6712373/2998271
Given UI virtualization working, LLS will request only sub set of items to be displayed (in other words, this[]
getter won't get invoked for all index available, only those to be displayed).
来源:https://stackoverflow.com/questions/22186076/how-to-do-data-virtualization-for-online-content-in-windows-phone