共用方式為


UIElement.ReleasePointerCapture(Pointer) 方法

定義

釋放指標擷取,以供這個 UIElement擷取一個特定指標。

public:
 virtual void ReleasePointerCapture(Pointer ^ value) = ReleasePointerCapture;
void ReleasePointerCapture(Pointer const& value);
public void ReleasePointerCapture(Pointer value);
function releasePointerCapture(value)
Public Sub ReleasePointerCapture (value As Pointer)

參數

value
Pointer

指標參考。 使用先前擷取或指標事件資料的已儲存參考來取得此參考。

範例

此範例示範 ReleasePointerCapture 根據處理 PointerReleased呼叫。

它也會顯示一開始擷取指標的程式碼,以及追蹤和計算指標參考的模式。

int _pointerCount;

public Scenario2()
{
    this.InitializeComponent();
    bEnteredExited.PointerEntered += bEnteredExited_PointerEntered;
    bEnteredExited.PointerExited += bEnteredExited_PointerExited;
    bEnteredExited.PointerPressed += bEnteredExited_PointerPressed;
    bEnteredExited.PointerReleased += bEnteredExited_PointerReleased;
    bEnteredExited.PointerMoved += bEnteredExited_PointerMoved;

    // To code for multiple Pointers (that is, fingers), 
    // we track how many entered/exited.
    _pointerCount = 0;
}

private void bEnteredExited_PointerMoved(object sender, 
    PointerRoutedEventArgs e)
{
    Scenario2UpdateVisuals(sender as Border, "Moved");
}

private void bEnteredExited_PointerReleased(object sender, 
    PointerRoutedEventArgs e)
{
    ((Border)sender).ReleasePointerCapture(e.Pointer);
    txtCaptureStatus.Text = string.Empty;
}

//Can only get capture on PointerPressed (i.e. touch down, mouse click, pen press)
private void bEnteredExited_PointerPressed(object sender, 
    PointerRoutedEventArgs e)
{
    if (tbPointerCapture.IsOn)
    {
        bool _hasCapture = ((Border)sender).CapturePointer(e.Pointer);
        txtCaptureStatus.Text = "Got Capture: " + _hasCapture;
    }
}

private void bEnteredExited_PointerExited(object sender, 
    PointerRoutedEventArgs e)
{
    _pointerCount--;
    Scenario2UpdateVisuals(sender as Border, "Exited");
}

private void bEnteredExited_PointerEntered(object sender, 
    PointerRoutedEventArgs e)
{
    _pointerCount++;
    Scenario2UpdateVisuals(sender as Border, "Entered");
}

private void Scenario2UpdateVisuals(Border border, 
    String eventDescription)
{
    switch (eventDescription.ToLower())
    {
        case "exited":
            if (_pointerCount <= 0)
            {
                border.Background = new SolidColorBrush(Colors.Red);
                bEnteredExitedTextBlock.Text = eventDescription;
            }
            break;
        case "moved":
            RotateTransform rt = 
                (RotateTransform)bEnteredExitedTimer.RenderTransform;
            rt.Angle += 2;
            if (rt.Angle > 360) rt.Angle -= 360;
            break;
        default:
            border.Background = new SolidColorBrush(Colors.Green);
            bEnteredExitedTextBlock.Text = eventDescription;
            break;
    }
}

private void Scenario2Reset(object sender, RoutedEventArgs e)
{
    Scenario2Reset();
}

private void Scenario2Reset()
{
    bEnteredExited.Background = new SolidColorBrush(Colors.Green);
    bEnteredExitedTextBlock.Text = string.Empty;
}

備註

以程式設計方式釋放指標擷取,不是元素可能會遺失指標擷取 ReleasePointerCapture 的唯一方式。 例如,使用者驅動事件,例如放開指標 (觸控點向上,放開滑鼠按鍵) 可能會導致指標擷取取消。

您可以接聽 PointerCaptureLost事件,以判斷何時發生此情況。

另一個可能會取消指標擷取的方式是,如果指標移出一個應用程式,並在兩個應用程式並存時移至另一個應用程式。

只有當您的應用程式程式碼先前稱為CapturePointer,而且具有您要釋放指標擷取的特定Pointer實例參考時,才應該呼叫 ReleasePointerCapture 。 您通常會透過PointerReleasedPointerMoved等事件取得該 Pointer 參考。

您的應用程式程式碼可能會從具有任何目前指標擷取的不同UIElement呼叫CapturePointer。 如果是,則會取消先前由其他元素建立的任何指標擷取。

UIElement可以擷取多個指標來處理多個觸控點,例如用於操作,但應用程式中只有一個 UIElement 指標可以一次擷取任何指標。 如需詳細資訊,請參閱 處理指標輸入

適用於

另請參閱