我正在创建一个 win 10 UWP 应用程序。
<Grid>
<WebView x:Name="WebViewElm"/>
</Grid>
我在其中添加了一个 webview,我需要为 webview 元素获取鼠标右键单击和拖放事件。但现在它正在为浏览器获取事件。我如何处理 webview 中的输入事件
最佳答案
如何将元素从 XAML 拖动到 WebView 的示例。这不是完整的解决方案,但可能对您有所帮助。
XAML:
<StackPanel Orientation="Vertical" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<WebView DefaultBackgroundColor="LightGray" Source="ms-appx-web:///HTMLPage1.html" PointerReleased="WebViewElm_PointerReleased" Width="300" Height="300" x:Name="WebViewElm"></WebView>
<TextBlock x:Name="txtZiel" DragOver="txtZiel_DragOver" PointerReleased="txtZiel_PointerReleased" >2</TextBlock>
</StackPanel>
C#代码:
private async void WebViewElm_PointerReleased(object sender, PointerRoutedEventArgs e)
{
await WebViewElm.InvokeScriptAsync("callFromExternal", new string[] { txtZiel.Text });
}
private async void WebViewElm_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
await WebViewElm.InvokeScriptAsync("LoadingFinished", null);
}
HTML:
<body>
<p id="txtClick">1</p>
</body>
JavaScript:
function callFromExternal(somedrag) {
document.getElementById("txtClick").innerHTML = somedrag;
}
您唯一需要检查的是将鼠标悬停在 JavaScript 代码中的段落元素上。
如果您想检查 JavaScript 代码中的事件,您可以将 NavigationCompleted="WebViewElm_NavigationCompleted" 和 ScriptNotify="WebViewWithJSInjection_ScriptNotify" 属性添加到您的 WebView 和 C# 事件中
private async void WebViewElm_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
await WebViewElm.InvokeScriptAsync("LoadingFinished", null);
}
private void WebViewWithJSInjection_ScriptNotify(object sender, NotifyEventArgs e)
{
// here in e.Value you can get string with document.getElementById("txtClick").innerHTML
}
在 JavaScript 中添加函数:
function LoadingFinished()
{
document.getElementById('txtClick').addEventListener("mousedown", callExternal);
}
function callExternal()
{
window.external.notify(document.getElementById("txtClick").innerHTML);
}
关于c# - 需要在 webview Win 10 UWP 中获取鼠标事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37155970/