VisualInteractionSource.TryRedirectForManipulation 方法

定义

重载

TryRedirectForManipulation(ExpPointerPoint)
TryRedirectForManipulation(PointerPoint)

尝试从给定 PointerPoint 开始,将给定指针流的配置操作发送到 InteractionTracker,而不是应用的 UI 线程。

重要

仅应使用 Touch 的 PointerDeviceType 调用此方法。

TryRedirectForManipulation(ExpPointerPoint)

public:
 virtual void TryRedirectForManipulation(ExpPointerPoint ^ pointerPoint) = TryRedirectForManipulation;
void TryRedirectForManipulation(ExpPointerPoint const& pointerPoint);
public void TryRedirectForManipulation(ExpPointerPoint pointerPoint);
function tryRedirectForManipulation(pointerPoint)
Public Sub TryRedirectForManipulation (pointerPoint As ExpPointerPoint)

参数

pointerPoint
ExpPointerPoint

适用于

TryRedirectForManipulation(PointerPoint)

尝试从给定 PointerPoint 开始,将给定指针流的配置操作发送到 InteractionTracker,而不是应用的 UI 线程。

重要

仅应使用 Touch 的 PointerDeviceType 调用此方法。

public:
 virtual void TryRedirectForManipulation(PointerPoint ^ pointerPoint) = TryRedirectForManipulation;
void TryRedirectForManipulation(PointerPoint const& pointerPoint);
public void TryRedirectForManipulation(PointerPoint pointerPoint);
function tryRedirectForManipulation(pointerPoint)
Public Sub TryRedirectForManipulation (pointerPoint As PointerPoint)

参数

pointerPoint
PointerPoint

输入点。

示例

以下代码片段演示如何配置视觉对象以遵循输入/手势。

这是通过三个主要步骤完成的:

  1. 创建 InteractionTracker 并在其边界) 设置 (或绑定。
  2. 创建至少一个 VisualInteractionSource 并将其与 InteractionTracker 相关联。
  3. 获取 InteractionTracker 的输出并将其应用于视觉对象的OffsetScale 属性。
  4. 指示系统尝试在 PointerPressed 发生时处理操作
void SetupSimpleInteractionTracker(Visual containerVisual, Visual contentVisual)
{
  //
  // Create the InteractionTracker and set its min/max position and scale.
  // These could also be bound to expressions.
  //
  // Note: 
  // The scrollable area can be changed from either end for content updates/virtualization.
  //

  _tracker = InteractionTracker.Create(_compositor);

  _tracker.MaxPosition = new Vector3(
  contentVisual.Size.X - containerVisual.Size.X,
  contentVisual.Size.Y - containerVisual.Size.Y,
  0.0f);

  _tracker.MinScale = 0.5f;
  _tracker.MaxScale = 4.0f;


  //
  // Configure the interaction source.  Enable input with inertia on all axes.
  //

  _interactionSource = VisualInteractionSource.Create(_compositor, containerVisual);

  _interactionSource.PositionXSourceMode = InteractionSourceMode.EnabledWithInertia;
  _interactionSource.PositionYSourceMode = InteractionSourceMode.EnabledWithInertia;
  _interactionSource.ScaleSourceMode = InteractionSourceMode.EnabledWithInertia;

  _tracker.InteractionSources.Add(_interactionSource);

  //
  // Register for the pointer pressed event so that we can tell the system to handle the
  // manipulations.
  //

  _rootElement.PointerPressed += OnPointerPressedBasic;

  //
  // Bind the InteractionTracker outputs to the contentVisual.
  //

  var positionExpression = _compositor.CreateExpressionAnimation("-tracker.Position");
  positionExpression.SetReferenceParameter("tracker", _tracker);

  contentVisual.StartAnimation("Offset", positionExpression);


  var scaleExpression = _compositor.CreateExpressionAnimation("Vector3(tracker.Scale, tracker.Scale, 1.0)");

  scaleExpression.SetReferenceParameter("tracker", _tracker);

  contentVisual.StartAnimation("Scale", scaleExpression);
}

private void OnPointerPressedBasic(object sender, PointerRoutedEventArgs e)
{
  //
  // Try to handle the manipulation in the system.
  //
  if (e.Pointer.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Touch)
  {       
    _interactionSource.TryRedirectForManipulation(
    e.CurrentPoint.GetCurrentPoint(_rootElement));
  }
}

Compositor _compositor = null;
InteractionTracker _tracker = null;
UIElement _rootElement = null;
VisualInteractionSource _interactionSource;

注解

此方法使应用能够指示系统 (Compositor) 应接管为给定 VisualInteractionSource 配置的任何操作。 调用 TryRedirectForManipulation 时,在 PointerPoint 中传递的 帧将用作 InteractionTracker 的输入处理的起点。 如果用户的手势已命中测试到与 VisualInteractionSource (或其子级) 关联的视觉对象,并且用户执行配置的手势,则系统将操作。 如果输入被命中测试到另一个视觉对象或用户执行未配置的手势,则输入将通过标准输入路由发送到 CoreWindow

如果 VisualInteractionSource 将输入流重新路由到 InteractionTracker,应用程序将在 UIElement 上接收 InputPointerSource.PointerCaptureLost,或 CoreWindow 上的 InputPointerSource.PointerRoutedAway。 这些消息指示输入已从应用程序 UI 线程发送。 此方法最常在 PointerPressed 上调用。

注意

PrecisionTouchpad 输入不需要此调用,如果 InputRedirectionMode 已设置为包含 CapableTouchpad 输入,则会自动路由到 InteractionTracker 以用于适当的手势。

异步行为

此系统的性质是异步的。 当应用调用 TryRedirectForManipulation 并且调用到达合成器时,将有其他输入事件可能已进入应用程序的 UI 线程。 在大多数情况下,应用收到这些事件并不有害,你可以选择在应用中进行手势检测,以确定如何处理输入。 应用将通过接收 PointerCaptureLost (ScrollViewer 接管输入处理) 时收到的相同事件来了解系统操作是否接管。 一个复杂情况是,像“快速轻拂”这样的手势 (短时间轻扫) 有可能在合成器响应之前将整个输入流(包括 PointerReleased)发送到应用程序。 如果你的应用受到这些限制的影响,你可能需要执行一些手势检测,以确保行为正确。

与 XAML 控件逻辑集成

在上述快速轻拂方案中,平移恰好位于按钮等控件上,如果平移包含在按钮内,该按钮将检测为单击。 这与执行路由的系统 GestureRecognizer 检测手势的方式略有不同。 为了确保快速轻拂类型方案在 XAML 中正常工作,你需要在按钮上侦听 OnTapped ,而不只是 单击 (如果按钮位于合成器交互区域内)。

合成器命中测试

用于确定操作是否在正确的视觉对象上发生的命中测试结果将基于合成器的命中测试。 合成器命中测试不如 XAML 命中测试可靠,因此在某些情况下,结果可能会略有差异。

与冲突的自定义手势识别器集成

在内部,系统 GestureRecognizer 用于路由输入。 应用通常可以获取所需的行为,只要它们使用系统手势识别。 如果你的应用尝试使用与系统识别器冲突的自定义识别器,则可能存在冲突,即应用希望合成器接管,但不会。

适用于