UIElement.PointerPressed 事件

定义

当指针设备在此元素中启动 Press 操作时发生。

public:
 virtual event PointerEventHandler ^ PointerPressed;
// Register
event_token PointerPressed(PointerEventHandler const& handler) const;

// Revoke with event_token
void PointerPressed(event_token const* cookie) const;

// Revoke with event_revoker
UIElement::PointerPressed_revoker PointerPressed(auto_revoke_t, PointerEventHandler const& handler) const;
public event PointerEventHandler PointerPressed;
function onPointerPressed(eventArgs) { /* Your code */ }
uIElement.addEventListener("pointerpressed", onPointerPressed);
uIElement.removeEventListener("pointerpressed", onPointerPressed);
- or -
uIElement.onpointerpressed = onPointerPressed;
Public Custom Event PointerPressed As PointerEventHandler 
<uiElement PointerPressed="eventhandler"/>

事件类型

注解

触摸、鼠标和笔/触笔交互在 UWP 应用中作为指针输入进行接收、处理和管理。 这些交互中的任何一个都可以生成 PointerPressed 事件。 有关详细信息,请参阅 处理指针输入。 指针事件适用于你对多个指针及其关系感兴趣的场景,或者检查每个指针的详细信息(例如确切坐标位置)。 否则,可以考虑处理手势事件,例如 Tappped

使用基于 PointerEventHandler 的处理程序来处理此事件。

鼠标输入与第一次检测到鼠标输入时分配的单个指针相关联。 单击鼠标按钮(左键、滚轮或右键)会通过 PointerPressed 事件在指针和该按钮之间创建一个辅助关联。 仅当释放该鼠标按钮时才引发 PointerReleased 事件(在完成该事件之前,其他按钮无法与指针关联)。 由于此排他性关联,会通过 PointerMoved 事件路由其他鼠标按钮单击。 处理此事件时,可以测试鼠标按钮状态,如以下示例所示。

private void Target_PointerMoved(object sender, PointerRoutedEventArgs e)
{
    Windows.UI.Xaml.Input.Pointer ptr = e.Pointer;

    // Multiple, simultaneous mouse button inputs are processed here.
    // Mouse input is associated with a single pointer assigned when 
    // mouse input is first detected. 
    // Clicking additional mouse buttons (left, wheel, or right) during 
    // the interaction creates secondary associations between those buttons 
    // and the pointer through the pointer pressed event. 
    // The pointer released event is fired only when the last mouse button 
    // associated with the interaction (not necessarily the initial button) 
    // is released. 
    // Because of this exclusive association, other mouse button clicks are 
    // routed through the pointer move event.          
    if (ptr.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Mouse)
    {
        // To get mouse state, we need extended pointer details.
        // We get the pointer info through the getCurrentPoint method
        // of the event argument. 
        Windows.UI.Input.PointerPoint ptrPt = e.GetCurrentPoint(Target);
        if (ptrPt.Properties.IsLeftButtonPressed)
        {
            eventLog.Text += "\nLeft button: " + ptrPt.PointerId;
        }
        if (ptrPt.Properties.IsMiddleButtonPressed)
        {
            eventLog.Text += "\nWheel button: " + ptrPt.PointerId;
        }
        if (ptrPt.Properties.IsRightButtonPressed)
        {
            eventLog.Text += "\nRight button: " + ptrPt.PointerId;
        }
    }

    // Prevent most handlers along the event route from handling the same event again.
    e.Handled = true;

    // Display pointer details.
    updateInfoPop(e);
}
private void Target_PointerMoved(object sender, PointerRoutedEventArgs e)
{
    Windows.UI.Xaml.Input.Pointer ptr = e.Pointer;

    // Multiple, simultaneous mouse button inputs are processed here.
    // Mouse input is associated with a single pointer assigned when 
    // mouse input is first detected. 
    // Clicking additional mouse buttons (left, wheel, or right) during 
    // the interaction creates secondary associations between those buttons 
    // and the pointer through the pointer pressed event. 
    // The pointer released event is fired only when the last mouse button 
    // associated with the interaction (not necessarily the initial button) 
    // is released. 
    // Because of this exclusive association, other mouse button clicks are 
    // routed through the pointer move event.          
    if (ptr.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Mouse)
    {
        // To get mouse state, we need extended pointer details.
        // We get the pointer info through the getCurrentPoint method
        // of the event argument. 
        Windows.UI.Input.PointerPoint ptrPt = e.GetCurrentPoint(Target);
        if (ptrPt.Properties.IsLeftButtonPressed)
        {
            eventLog.Text += "\nLeft button: " + ptrPt.PointerId;
        }
        if (ptrPt.Properties.IsMiddleButtonPressed)
        {
            eventLog.Text += "\nWheel button: " + ptrPt.PointerId;
        }
        if (ptrPt.Properties.IsRightButtonPressed)
        {
            eventLog.Text += "\nRight button: " + ptrPt.PointerId;
        }
    }

    // Prevent most handlers along the event route from handling the same event again.
    e.Handled = true;

    // Display pointer details.
    updateInfoPop(e);
}

鼠标输入设备中的指针事件在应用生存期的事件详细信息中通常具有相同的 PointerId

PointerPressed 是路由事件。 有关路由事件概念的详细信息,请参阅 事件和路由事件概述

对于触摸操作以及特定于交互的事件或一个触摸操作引起的操作事件,一个元素必须对点击测试可见,以用作事件源并触发与该操作关联的事件。 UIElement.Visibility 必须为 Visible。 派生类型的其他属性也会影响命中测试可见性。 有关详细信息,请参阅事件和路由事件概述

PointerPressed 支持将事件处理程序附加到将调用的路由,即使事件的事件数据标记为“ 已处理”也是如此。 请参阅 AddHandler

特定Windows 运行时控件可能对 PointerPressed 输入事件具有基于类的处理。 如果是这样,控件可能具有 OnPointerPressed 方法的替代。 通常,事件标记为由类处理程序处理,并且不会引发 PointerPressed 事件,以便由该控件上的任何用户代码处理程序进行处理。 例如, ButtonBase 具有处理 PointerPressed 并触发 Click 的类处理。 有关事件基于类的处理工作原理的详细信息,请参阅 事件和路由事件概述

控件可能还具有独立于事件运行的 PointerDownThemeAnimation 个性动画。

指针捕获

在捕获指针时,PointerPressed 有时用作启动事件,因此,只要指针保持向下状态,特定元素将捕获与指针相关的其他事件。 若要捕获指针,请对应维护捕获的特定 UI 元素调用 CapturePointer 方法。 这通常在 PointerPressed 事件处理程序中完成,并且你对事件的发送方调用 CapturePointer。 有关如何捕获指针以及为何要捕获指针的详细信息,请参阅 CapturePointer

PointerPressed 和手势事件,或操作事件

除非输入设备是鼠标,否则最初触发 PointerPressed 事件的用户操作最终会导致表示保持手势的 Hold 事件。 如果发生 PointerPressed 的元素具有非默认 ManipulationMode 值,则该操作还可能导致各种操作事件,如 ManipulationStarted。 有关详细信息,请参阅 处理指针输入中的“使用操作事件”部分。

当 PointerPressed 触发时, 诸如 Tapped 之类的手势事件不会触发,因为 除 Holding 以外的手势事件正在等待指针释放,然后再触发事件。

作为应用的用户交互的一般设计规则,应检查是否有可用于交互的特定于控件的事件或适当的手势事件。 例如,如果控件是 Button,则该控件具有一个 Click 事件,该事件专门用于用户调用按钮的操作。 或者,如果元素不是按钮,但你要处理元素的主要操作和事件,则可以处理 Tappped

可以通过设置 IsTapEnabled 等属性,在单个元素上专门禁用手势事件。 如果正在处理操作,可以禁用手势事件,但如果正在处理指针事件,通常不需要禁用手势事件。 不能专门禁用指针事件,但可以选择不处理它们。

PointerPressed 和 PointerReleased

其他事件(而不是 PointerReleased )可能会在操作结束时触发,例如 PointerCanceledPointerCaptureLost。 不要依赖于 PointerPressed 和 PointerReleased 事件始终成对发生。 若要正常运行,你的应用必须侦听并处理代表 “按下” 操作可能结论的所有事件。 可能无法获得 PointerReleased 出现的一些原因包括:

  • 特定硬件处理触摸操作和 按下 操作的方式的差异
  • 从其他指针进行编程指针捕获
  • 用于更改显示区域关系的用户操作,例如更改分辨率或监视器设置
  • 输入交互,例如触笔触摸与上一个触摸操作相同的表面

适用于

另请参阅