Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc - Only a single WebView2DevToolsContext should exist at any given time #11

Open
martinweihrauch opened this issue Dec 6, 2022 · 2 comments

Comments

@martinweihrauch
Copy link

I have a weird problem (and can share you the source, if you like).

I have a WPF with a webview.
I have a Click Button for users to navigate to a URL:

private void Button_Go_Click(object sender, RoutedEventArgs e)
{
if (!_handlerAttached)
{
webView.CoreWebView2.DOMContentLoaded += WebView_NavigationCompleted;
_handlerAttached = true;
}
if (webView != null && webView.CoreWebView2 != null)
{
webView.CoreWebView2.Navigate(tbUrl.Text);
}
}

private async void WebView_NavigationCompleted(object sender, CoreWebView2DOMContentLoadedEventArgs e)
{
var text = await webView.ExecuteScriptAsync("document.body.innerHTML");
var devToolsContext = await webView.CoreWebView2.CreateDevToolsContextAsync();
await Handle(devToolsContext);
}

public async Task Handle(WebView2DevToolsContext dtc)
{

        var body = await dtc.QuerySelectorAsync<HtmlElement>("body");
        if (body != null)
        {
            
        }

Now the weird bug:

In the application, I click on the button, then it correctly executes the method "WebView_NavigationCompleted" and then the method "Handle".
If I click again, it executes the method WebView_NavigationCompleted and jumps over the method Handle .
If I click again, then it first executes the Handle method and only then goes into WebView_NavigationCompleted.

Do you have any explanation?

@martinweihrauch
Copy link
Author

I have identified the problem:

You have to run this:
await dtc.DisposeAsync();

If you don't dispose the object, even if the method ended and everything, it still exists and causes problems.

@amaitland
Copy link
Collaborator

When posting code please use markdown formatting to make it readable, see https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks

Only a single WebView2DevToolsContext should exist at any given time, when you are finished them make sure you dispose via DisposeAsync.

As per https://github.com/ChromiumDotNet/WebView2.DevTools.Dom#webview2devtoolscontext

The xml doc should be updated to better document this.

If you wish to re-use a WebView2DevToolsContext then that's fine, just make sure there is only one.

You should be able to use the WebView2.CoreWebView2InitializationCompleted Event
also.

@amaitland amaitland changed the title Bug? Doc - Only a single WebView2DevToolsContext should exist at any given time Dec 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants