【问题标题】:RelativePanel PointerPressed Event Won't be fired in UWP?RelativePanel PointerPressed 事件不会在 UWP 中触发?
【发布时间】:2020-10-21 16:04:22
【问题描述】:

我尝试的是什么?

XAML 代码:

<Canvas x:Name="grid" Width="500" Height="500">
     <RelativePanel PointerPressed="RelativePanel_PointerPressed" Canvas.Left="100" Canvas.Top="100" Background="Green" Height="100">
        <RichEditBox x:Name="box" Width="100" Height="32" ></RichEditBox>
        <Rectangle RelativePanel.AlignBottomWithPanel="True" RelativePanel.AlignBottomWith="box" Width="100" Height="10" Fill="Blue" Margin="0 15 0 20"></Rectangle>
    </RelativePanel>
</Canvas>

C#代码:

   private void RelativePanel_PointerPressed(object sender, PointerRoutedEventArgs e)
    {
     Debug.WriteLine("Hello");
   }

RichEditBox 和 rectangle 是 RelativePanel 的子级。

当我点击矩形时,RelativePanel 的 pointerPressed 事件已正确触发。

但是,当我点击 RichEditBox 时,该事件不会被触发。我想在任何地方触发该事件,我在相关面板内点击。如何实现这个?

【问题讨论】:

  • 您的问题解决了吗?如果还没有解决,请随时联系我们。
  • 是的。它解决了。谢谢

标签: uwp uwp-xaml


【解决方案1】:

但是,当我点击 RichEditBox 时,该事件不会被触发。我想在任何地方触发该事件,我在相关面板内点击。如何实现这个?

您可以使用AddHandler event handing technique 指定您希望调用PointerPressed 事件处理程序,即使该事件被标记为使用设置为true 的第三个参数处理,如下所示:

box.AddHandler(RichEditBox.PointerReleasedEvent, new PointerEventHandler(RelativePanel_PointerPressed), true);

但是,您可能还注意到 document 表示“不要经常要求重新处理路由事件,因为它会干扰 Windows 运行时事件系统的预期设计以进行控制合成。”

除了上面的方法,我们还可以使用PointerCaptureLost事件来替换PointerPressed事件,点击RichEditBox控件即可调用。例如:

<Canvas x:Name="grid" Width="500" Height="500">
    <RelativePanel PointerPressed="RelativePanel_PointerPressed"
                   Canvas.Left="100" Canvas.Top="100" Background="Green" Height="100">
        <RichEditBox x:Name="box" Width="100" Height="32" PointerCaptureLost="RelativePanel_PointerCaptureLost"/>
        <Rectangle RelativePanel.AlignBottomWithPanel="True" RelativePanel.AlignBottomWith="box"
                   Width="100" Height="10" Fill="Blue" Margin="0 15 0 20"/>
    </RelativePanel>
</Canvas>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多