UIElement.CapturePointer(Pointer) 方法

定义

将指针捕获设置为 UIElement。 捕获后,只有具有捕获的元素才会触发与指针相关的事件。

public:
 virtual bool CapturePointer(Pointer ^ value) = CapturePointer;
bool CapturePointer(Pointer const& value);
public bool CapturePointer(Pointer value);
function capturePointer(value)
Public Function CapturePointer (value As Pointer) As Boolean

参数

value
Pointer

指针对象引用。

返回

Boolean

bool

如果对象具有指针捕获,则为 true;否则为 false

示例

此示例演示如何基于处理 PointerPressed 调用 CapturePointer。 它还显示了用于跟踪和计数指针引用的模式。

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;
}
Private _pointerCount As Integer

Public Sub New()
    Me.InitializeComponent()
    AddHandler bEnteredExited.PointerEntered, AddressOf bEnteredExited_PointerEntered
    AddHandler bEnteredExited.PointerExited, AddressOf bEnteredExited_PointerExited
    AddHandler bEnteredExited.PointerPressed, AddressOf bEnteredExited_PointerPressed
    AddHandler bEnteredExited.PointerReleased, AddressOf bEnteredExited_PointerReleased
    AddHandler bEnteredExited.PointerMoved, AddressOf bEnteredExited_PointerMoved

    'To code for multiple Pointers (i.e. Fingers) we track how many entered/exited.
    _pointerCount = 0
End Sub

''' <summary>
''' Invoked when this page is about to be displayed in a Frame.
''' </summary>
''' <param name="e">Event data that describes how this page was reached.  The Parameter
''' property is typically used to configure the page.</param>
Protected Overrides Sub OnNavigatedTo(e As NavigationEventArgs)
End Sub

Private Sub bEnteredExited_PointerMoved(sender As Object, e As PointerRoutedEventArgs)
    Scenario2UpdateVisuals(TryCast(sender, Border), "Moved")
End Sub

Private Sub bEnteredExited_PointerReleased(sender As Object, e As PointerRoutedEventArgs)
    DirectCast(sender, Border).ReleasePointerCapture(e.Pointer)
    txtCaptureStatus.Text = String.Empty
End Sub

'Can only get capture on PointerPressed (i.e. touch down, mouse click, pen press)
Private Sub bEnteredExited_PointerPressed(sender As Object, e As PointerRoutedEventArgs)
    If tbPointerCapture.IsOn Then
        Dim _hasCapture As Boolean = DirectCast(sender, Border).CapturePointer(e.Pointer)
        txtCaptureStatus.Text = "Got Capture: " & _hasCapture
    End If
End Sub

Private Sub bEnteredExited_PointerExited(sender As Object, e As PointerRoutedEventArgs)
    _pointerCount -= 1
    Scenario2UpdateVisuals(TryCast(sender, Border), "Exited")
End Sub

Private Sub bEnteredExited_PointerEntered(sender As Object, e As PointerRoutedEventArgs)
    _pointerCount += 1
    Scenario2UpdateVisuals(TryCast(sender, Border), "Entered")

End Sub

Private Sub Scenario2UpdateVisuals(border As Border, eventDescription As String)
    Select Case eventDescription.ToLower()
        Case "exited"
            If _pointerCount <= 0 Then
                border.Background = New SolidColorBrush(Colors.Red)
                bEnteredExitedTextBlock.Text = eventDescription
            End If
            Exit Select
        Case "moved"

            Dim rt As RotateTransform = DirectCast(bEnteredExitedTimer.RenderTransform, RotateTransform)
            rt.Angle += 2
            If rt.Angle > 360 Then
                rt.Angle -= 360
            End If
            Exit Select
        Case Else
            border.Background = New SolidColorBrush(Colors.Green)
            bEnteredExitedTextBlock.Text = eventDescription
            Exit Select

    End Select
End Sub

Private Sub Scenario2ResetMethod(sender As Object, e As RoutedEventArgs)
    Reset()
End Sub

Private Sub Reset()
    bEnteredExited.Background = New SolidColorBrush(Colors.Green)
    bEnteredExitedTextBlock.Text = String.Empty
End Sub

注解

仅当指针处于按下状态时,才能成功捕获指针 (Pointer.IsInContact 应为 true) 。 按下的物理构成因按下的指针设备类型 (按下鼠标按钮、向下触摸点、接触) 触笔而异。 如果尝试捕获未按下的指针,或者之前按下指针但现已释放,则 CapturePointer 将返回 false。 现有捕获不受返回 false 的 CapturePointer 调用的影响。

通常捕获 PointerPressed 事件处理程序中的指针。 从 PointerPressed 处理程序的 PointerRoutedEventArgs 事件数据获取的 Pointer 实例是从处理程序的代码中调用 CapturePointer 时应为 value 参数传递的值。

通常捕获指针是因为希望当前指针操作在应用中启动行为。 在这种情况下,通常不希望其他元素处理来自该指针操作的任何其他事件,直到你的行为完成或通过释放指针捕获取消为止。 如果捕获指针,则只有具有捕获的元素才能获取指针的输入事件,并且其他元素不会触发事件,即使指针移动到其边界内。 例如,假设 UI 具有两个相邻元素。 通常,如果将指针从一个元素移动到另一个元素,则首先从第一个元素获取 PointerMoved 事件,然后从第二个元素获取 PointerMoved 事件。 但是,如果第一个元素已捕获指针,则第一个元素将继续接收 PointerMoved 事件,即使捕获的指针离开了其边界。 此外,当捕获的指针进入捕获的指针时,第二个元素不会为捕获的指针触发 PointerEntered 事件。

指针捕获状态和生成与指针捕获相关的事件并不完全由应用代码决定。 如果用户释放指针,则会生成 PointerReleased 事件,并且与该指针关联的指针捕获将丢失。 这也会在原始捕获元素上触发 PointerCaptureLost

在大多数情况下,当用户完成释放上一个指针捕获的输入操作 (抬起触摸点、松开鼠标左键、将触笔移出范围) 时,将自动释放指针捕获。 可能释放捕获的另一个条件是也会触发 PointerCanceled 事件的任何操作。 你的应用通常可以依赖于与用户输入操作关联的捕获-发布行为,而无需使用 ReleasePointerCaptureReleasePointerCaptures 专门取消指针捕获。 有关详细信息,请参阅鼠标交互

如果已捕获指针,则 CapturePointer 方法将返回 false

UIElement 一次可以捕获多个指针点。 使用 value 参数指示要捕获的 Pointer 实例。

表示笔势 ((如 TapppedDoubleTapped) )的输入事件通常仅在释放指针后触发,因此不应尝试在手势事件的事件处理程序中捕获指针。 不允许手势事件的事件数据中的 指针 引用启动指针捕获。

提示

不要尝试在指针相关的输入事件处理程序范围之外使用 CapturePointer。 除非你有一个 指针 ,你确定它与当时允许进行指针捕获的指针相关联,否则 CapturePointer 调用不会有任何影响。 没有实际方法可以生成新的指针并使用该新 指针 调用 CapturePointer。 应仅使用系统通过 指针 相关输入事件的事件数据提供的指针引用。

适用于

另请参阅