2012-08-22 85 views
0

MSDN上的這個頁面代表了一個使用HitTest的例子,這個例子在概念上是清晰的,但是我沒有得到的是C#代碼指向hitResultsList的列表。我試圖將其聲明爲List:使用HitTest中的列表

List<myShapeClass> hitResultsList = new List<myShapeClass>(); 

但是,我收到了類型轉換錯誤。任何幫助將不勝感激。問題是我應該用什麼樣的List來進行HitTesting?

代碼是在這裏:

// Respond to the right mouse button down event by setting up a hit test results callback. 
private void OnMouseRightButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    // Retrieve the coordinate of the mouse position. 
    Point pt = e.GetPosition((UIElement)sender); 

    // Clear the contents of the list used for hit test results. 
    hitResultsList.Clear(); 

    // Set up a callback to receive the hit test result enumeration. 
    VisualTreeHelper.HitTest(myCanvas, null, 
     new HitTestResultCallback(MyHitTestResult), 
     new PointHitTestParameters(pt)); 

    // Perform actions on the hit test results list. 
    if (hitResultsList.Count > 0) 
    { 
     Console.WriteLine("Number of Visuals Hit: " + hitResultsList.Count); 
    } 
} 

感謝。

回答

2

的關鍵就在這裏,在回調:

// Return the result of the hit test to the callback. 
public HitTestResultBehavior MyHitTestResult(HitTestResult result) 
{ 
    // Add the hit test result to the list that will be processed after the enumeration. 
    hitResultsList.Add(result.VisualHit); 

    // Set the behavior to return visuals at all z-order levels. 
    return HitTestResultBehavior.Continue; 
} 

通知是添加result.VisualHit,並resultHitTestResult。所以,如果你查看那個成員(http://msdn.microsoft.com/en-us/library/system.windows.media.hittestresult.visualhit.aspx),你會看到它是一個DependencyObject

所以你想:List<DependencyObject>