ListView.VirtualMode 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定一個值,表示你是否為控制系統提供了自己的資料管理操作 ListView 。
public:
property bool VirtualMode { bool get(); void set(bool value); };
public bool VirtualMode { get; set; }
member this.VirtualMode : bool with get, set
Public Property VirtualMode As Boolean
屬性值
true如果ListView使用你所提供的資料管理操作;否則, false 預設值為 false。
例外狀況
VirtualMode 設定為 true ,且存在以下其中一個條件:
VirtualListSize 大於 0 且 RetrieveVirtualItem 不被處理。
-或-
Items, , CheckedItems, SelectedItems 或包含物品。
-或-
編輯為 Items。
範例
此範例說明一個內容為前萬格的簡單體 ListView 。 它負責搜尋並使用快取以提升效能。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form
{
private ListViewItem[] myCache; //array to cache items for the virtual list
private int firstItem; //stores the index of the first item in the cache
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
public Form1()
{
//Create a simple ListView.
ListView listView1 = new ListView();
listView1.View = View.SmallIcon;
listView1.VirtualMode = true;
listView1.VirtualListSize = 10000;
//Hook up handlers for VirtualMode events.
listView1.RetrieveVirtualItem += new RetrieveVirtualItemEventHandler(listView1_RetrieveVirtualItem);
listView1.CacheVirtualItems += new CacheVirtualItemsEventHandler(listView1_CacheVirtualItems);
listView1.SearchForVirtualItem += new SearchForVirtualItemEventHandler(listView1_SearchForVirtualItem);
//Add ListView to the form.
this.Controls.Add(listView1);
//Search for a particular virtual item.
//Notice that we never manually populate the collection!
//If you leave out the SearchForVirtualItem handler, this will return null.
ListViewItem lvi = listView1.FindItemWithText("111111");
//Select the item found and scroll it into view.
if (lvi != null)
{
listView1.SelectedIndices.Add(lvi.Index);
listView1.EnsureVisible(lvi.Index);
}
}
//The basic VirtualMode function. Dynamically returns a ListViewItem
//with the required properties; in this case, the square of the index.
void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
//Caching is not required but improves performance on large sets.
//To leave out caching, don't connect the CacheVirtualItems event
//and make sure myCache is null.
//check to see if the requested item is currently in the cache
if (myCache != null && e.ItemIndex >= firstItem && e.ItemIndex < firstItem + myCache.Length)
{
//A cache hit, so get the ListViewItem from the cache instead of making a new one.
e.Item = myCache[e.ItemIndex - firstItem];
}
else
{
//A cache miss, so create a new ListViewItem and pass it back.
int x = e.ItemIndex * e.ItemIndex;
e.Item = new ListViewItem(x.ToString());
}
}
//Manages the cache. ListView calls this when it might need a
//cache refresh.
void listView1_CacheVirtualItems(object sender, CacheVirtualItemsEventArgs e)
{
//We've gotten a request to refresh the cache.
//First check if it's really neccesary.
if (myCache != null && e.StartIndex >= firstItem && e.EndIndex <= firstItem + myCache.Length)
{
//If the newly requested cache is a subset of the old cache,
//no need to rebuild everything, so do nothing.
return;
}
//Now we need to rebuild the cache.
firstItem = e.StartIndex;
int length = e.EndIndex - e.StartIndex + 1; //indexes are inclusive
myCache = new ListViewItem[length];
//Fill the cache with the appropriate ListViewItems.
int x = 0;
for (int i = 0; i < length; i++)
{
x = (i + firstItem) * (i + firstItem);
myCache[i] = new ListViewItem(x.ToString());
}
}
//This event handler enables search functionality, and is called
//for every search request when in Virtual mode.
void listView1_SearchForVirtualItem(object sender, SearchForVirtualItemEventArgs e)
{
//We've gotten a search request.
//In this example, finding the item is easy since it's
//just the square of its index. We'll take the square root
//and round.
double x = 0;
if (Double.TryParse(e.Text, out x)) //check if this is a valid search
{
x = Math.Sqrt(x);
x = Math.Round(x);
e.Index = (int)x;
}
//If e.Index is not set, the search returns null.
//Note that this only handles simple searches over the entire
//list, ignoring any other settings. Handling Direction, StartIndex,
//and the other properties of SearchForVirtualItemEventArgs is up
//to this handler.
}
}
Public Class Form1
Inherits Form
Private myCache() As ListViewItem 'array to cache items for the virtual list
Private firstItem As Integer 'stores the index of the first item in the cache
Private WithEvents listView1 As ListView
Public Shared Sub Main()
Application.Run(New Form1)
End Sub
Public Sub New()
'Create a simple ListView.
listView1 = New ListView()
listView1.View = View.SmallIcon
listView1.VirtualMode = True
listView1.VirtualListSize = 10000
'Add ListView to the form.
Me.Controls.Add(listView1)
'Search for a particular virtual item.
'Notice that we never manually populate the collection!
'If you leave out the SearchForVirtualItem handler, this will return null.
Dim lvi As ListViewItem = listView1.FindItemWithText("111111")
'Select the item found and scroll it into view.
If Not (lvi Is Nothing) Then
listView1.SelectedIndices.Add(lvi.Index)
listView1.EnsureVisible(lvi.Index)
End If
End Sub
'The basic VirtualMode function. Dynamically returns a ListViewItem
'with the required properties; in this case, the square of the index.
Private Sub listView1_RetrieveVirtualItem(ByVal sender As Object, ByVal e As RetrieveVirtualItemEventArgs) Handles listView1.RetrieveVirtualItem
'Caching is not required but improves performance on large sets.
'To leave out caching, don't connect the CacheVirtualItems event
'and make sure myCache is null.
'check to see if the requested item is currently in the cache
If Not (myCache Is Nothing) AndAlso e.ItemIndex >= firstItem AndAlso e.ItemIndex < firstItem + myCache.Length Then
'A cache hit, so get the ListViewItem from the cache instead of making a new one.
e.Item = myCache((e.ItemIndex - firstItem))
Else
'A cache miss, so create a new ListViewItem and pass it back.
Dim x As Integer = e.ItemIndex * e.ItemIndex
e.Item = New ListViewItem(x.ToString())
End If
End Sub
'Manages the cache. ListView calls this when it might need a
'cache refresh.
Private Sub listView1_CacheVirtualItems(ByVal sender As Object, ByVal e As CacheVirtualItemsEventArgs) Handles listView1.CacheVirtualItems
'We've gotten a request to refresh the cache.
'First check if it's really neccesary.
If Not (myCache Is Nothing) AndAlso e.StartIndex >= firstItem AndAlso e.EndIndex <= firstItem + myCache.Length Then
'If the newly requested cache is a subset of the old cache,
'no need to rebuild everything, so do nothing.
Return
End If
'Now we need to rebuild the cache.
firstItem = e.StartIndex
Dim length As Integer = e.EndIndex - e.StartIndex + 1 'indexes are inclusive
myCache = New ListViewItem(length) {}
'Fill the cache with the appropriate ListViewItems.
Dim x As Integer = 0
Dim i As Integer
For i = 0 To length
x = (i + firstItem) * (i + firstItem)
myCache(i) = New ListViewItem(x.ToString())
Next i
End Sub
'This event handler enables search functionality, and is called
'for every search request when in Virtual mode.
Private Sub listView1_SearchForVirtualItem(ByVal sender As Object, ByVal e As SearchForVirtualItemEventArgs) Handles listView1.SearchForVirtualItem
'We've gotten a search request.
'In this example, finding the item is easy since it's
'just the square of its index. We'll take the square root
'and round.
Dim x As Double = 0
If [Double].TryParse(e.Text, x) Then 'check if this is a valid search
x = Math.Sqrt(x)
x = Math.Round(x)
e.Index = Fix(x)
End If
'Note that this only handles simple searches over the entire
'list, ignoring any other settings. Handling Direction, StartIndex,
'and the other properties of SearchForVirtualItemEventArgs is up
'to this handler.
End Sub
End Class
備註
將屬性設 VirtualMode 為 會 true 進入 ListView 虛擬模式。 在虛擬模式下,正常 Items 的收藏不會被使用。 相反地, ListViewItem 物件會依需求 ListView 動態建立。
虛擬模式在許多情況下都很有用。 如果 ListView 物件必須從記憶體中已經存在的龐大集合中填充,為每個項目建立 ListViewItem 物件可能會造成浪費。 在虛擬模式下,只會產生所需的物品。 在其他情況下,物件的值 ListViewItem 可能需要頻繁重新計算,若對整個集合進行此計算,將產生不可接受的效能。 在虛擬模式下,只會計算所需的項目。
要使用虛擬模式,你必須處理 RetrieveVirtualItem 事件,每當需要 ListView 物品時事件就會升起。 這個事件處理器應該建立 ListViewItem 屬於指定索引的物件。 此外, VirtualListSize 屬性必須設定為虛擬清單的大小。
處理事件 SearchForVirtualItem 後,虛擬模式中可進行搜尋。 若未處理此事件, FindItemWithText 與 FindNearestItem 方法將回傳 null。
你可以處理事件 CacheVirtualItems 來維持物件快取 ListViewItem 。 如果計算或查詢建立 ListViewItem 物件的成本很高,維護快取可以提升效能。
若 View 屬性設為 Tile,值將自動改為 LargeIcon ,當 VirtualMode 為 true時。
在虛擬模式下,收藏 Items 會被停用。 嘗試存取時會出現 InvalidOperationException. CheckedItems收藏與收藏SelectedItems同樣如此。 如果你想檢索選取或勾選的項目,請改用 SelectedIndices 和 CheckedIndices collections。