需要在webview Win 10 UWP中获取鼠标事件

时间:2016-05-11 07:33:53

标签: c# events webview win-universal-app windows-10

我正在创建一个win 10 UWP应用程序。

<Grid>
<WebView x:Name="WebViewElm"/>
</Grid>

我在其中添加了一个webview,我需要右键单击鼠标并拖动webview元素的drop事件。但现在它正在为浏览器拍摄事件。我如何处理webview中的输入事件

1 个答案:

答案 0 :(得分:1)

如何将元素从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代码中的事件,可以在WebView和C#事件中添加 NavigationCompleted =“WebViewElm_NavigationCompleted” ScriptNotify =“WebViewWithJSInjection_ScriptNotify”属性

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);
    }