简体   繁体   English

在 UWP 应用程序的同一网格内过滤 WebView 和其他 UI 元素之间的事件

[英]Filtering events between WebView and other UI Elements inside the same grid of a UWP application

I'm trying to build a UWP-based WebView application, and I have this basic design:我正在尝试构建一个基于 UWP 的 WebView 应用程序,并且我有这个基本设计:

<Page>
  <Grid>
    <WebView Name="_webhost" DragEnter="_webhost_OnDragEnter"/>
    <StackPanel AllowDrop="True" Drop="_webhost_OnDrop" DragOver="UIElement_OnDragOver" Background="Transparent" PointerEntered="UIElement_OnPointerEntered"/>
  </Grid>
</Page>

So the StackPanel is on top of the Webview , both of them are filling the whole screen while the StackPanel is actually transparent by default.因此StackPanel位于Webview 之上,它们都填满了整个屏幕,而StackPanel默认情况下实际上是透明的。

When I set IsHitTestVisible=false in the StackPanel , then all mouse-related input events are fired in the WebView 's contents - but the thing is, I want some (specific) events to fire in the StackPanel.当我在StackPanel中设置IsHitTestVisible=false时,所有与鼠标相关的输入事件都会在WebView的内容中触发 - 但问题是,我希望在 StackPanel 中触发一些(特定)事件。 When not setting that property in the StackPanel , it catches all of the input events, thus none of them are propagated into the WebView .当不在StackPanel中设置该属性时,它会捕获所有输入事件,因此它们都不会传播到WebView中。

Note that setting event handlers for the WebView usually doesn't do anything - since official docs says state that it doesn't support most of the user input events inherited from UIElement.请注意,为WebView设置事件处理程序通常不会做任何事情 - 因为官方文档说state 它不支持从 UIElement 继承的大多数用户输入事件。

So bottom line - I want specific events to fire in StackPanel , but all other events to fire in the other element ( WebView or at least its contents).所以底线 - 我希望在StackPanel中触发特定事件,但在其他元素( WebView或至少其内容)中触发所有其他事件。 How can this be achieved?如何做到这一点?

In general, the input event will bubble from the children element to parent element, and the bottom same level control will not hold the event.一般情况下,输入事件会从子元素冒泡到父元素,底层同级控件不会持有该事件。 And I tested your code the WebView DragEnter never trigger until set IsHitTestVisible=false for StackPanel .我测试了您的代码 WebView DragEnter永远不会触发,直到为StackPanel设置IsHitTestVisible=false

I want specific events to fire in StackPanel我希望在 StackPanel 中触发特定事件

If you want specific the event for StackPanel , you could add e.Handled = true;如果你想为StackPanel指定事件,你可以添加e.Handled = true; in the event handler.在事件处理程序中。 But it only works for event that bubble from the children element to parent element但它仅适用于从子元素冒泡到父元素的事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM