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

指针引用。 使用先前捕获中保存的引用或指针事件数据来获取此引用。

示例

此示例显示基于处理 PointerReleased 的调用ReleasePointerCapture

它还显示了最初捕获指针的代码,以及用于跟踪和计数指针引用的模式。

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 不同的 UIElement 调用 CapturePointer。 如果是这样,则会取消以前由其他元素进行的任何指针捕获。

UIElement可以捕获多个指针来处理多个触摸点,例如用于操作,但一个应用中只有一个UIElement指针捕获在任何时间。 有关详细信息,请参阅 处理指针输入

适用于

另请参阅