Search functionality in WebView2

Viewed 783

Thanks in Advance !

In my application, I have embedded a WebView2 control inside a WPF usercontrol.

Is there anyway or a workaround that can help me to search text in the WebView2 control ? I see that the search text box opens up through Ctrl + F . My requirement is to call this functionality through the custom button click.

I identified that the current WebView2 pre-release SDK provided by Microsoft does'nt have any thing specific to find or search functionality.

2 Answers

WebView2 currently has no support for you invoking or otherwise controlling the find in page dialog and I don't know of any workaround other than implementing your own find in page style control in your HTML document.

If you like, you can open a Feature Request issue on our WebView2 Feedback page. Please let us know exactly what you want to be able to do with the find in page dialog and why. Thanks!

I've achieved it by using the Winform class SendKeys and its method SendWait. In order to being able to use a Winform class in a WPF app, you first must enable WindowsForms in the project setup PropertyGroup, by adding the UseWindowsForms tag

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWPF>true</UseWPF>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>

then, create your click method in the code behind. There, the first step is to pass the focus to the WebView2 element, so that the browser is the active element when the key simulation is performed.

this.browser.Focus();

And then, call the SendKeys.SendWait method with "{F3}" as argument

System.Windows.Forms.SendKeys.SendWait("{F3}");

So, the final look is this:

private void openSearch(object sender, RoutedEventArgs e)
{
    this.browser.Focus();
    System.Windows.Forms.SendKeys.SendWait("{F3}");
}

and that's it. Create your button in wpp, add the above method to its click property, run the app and press it. The browser search feature will open. See the sample below

screenshot

Related