how to get a reference to WebView2 control in VisualStudio.Extensibility tool window

Shalom Ben Zvi 0 Reputation points
2025-01-28T20:04:57.0933333+00:00

Hello,

I'm starting a Visual Studio extension where currently I need to embed WebView2 in a tool window. I need to interact with the webview by sending and receiving js messages. and loading resources. I need a reference to the WebView2 control

in a VisualStudio.Extensibility extension I added the WebView2 in the tool window xaml like that:

<wv2:WebView2
    Name="webView2"
    Visibility="Visible"
    HorizontalAlignment="Stretch"
    VerticalAlignment="Stretch">
    <wv2:WebView2.CreationProperties>
        <wv2:CoreWebView2CreationProperties UserDataFolder="C:\temp\myFolder"/>
    </wv2:WebView2.CreationProperties>
    <wv2:WebView2.Source>https://bing.com</wv2:WebView2.Source>
</wv2:WebView2>

It works. the web view shows on the tool window.

but I could not find a way to get a reference to the WebView2 control.

or a way to install listeners in the xaml and bind them to a control class.

In a VisualStudio.Extensibility extension the tool window has a RemoteUserControl associated with it and there is a way to bind data and commands, i couldn't find a way to bind the WebView2 instance.

in a VSSDK extension i managed to do all that, I could freely create the WebView2 instance and set it as the content of the tool window. or add it in the tool window control xaml with a Name attribute and a reference is generated in the xaml.cs class.

I also saw that its possible to bind listeners in the xaml like that

<wv2:WebView2
    Name="webView2"
    NavigationStarting="WebView_NavigationStarting"
    <wv2:WebView2.Source>https://bing.com</wv2:WebView2.Source>
</wv2:WebView2>

I did it in a VSSDK extension, WebView_NavigationStarting is a listener on the xaml.cs class.

but i can't find a way to do it in a VisualStudio.Extensibility extension, i don't know how and where to bind the WebView_NavigationStarting.

Visual Studio Extensions
Visual Studio Extensions
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Extensions: A program or program module that adds functionality to or extends the effectiveness of a program.
247 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.