question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Synchronous prompt on CustomScriptDialogOpening WebView2

See original GitHub issue

Description

I’m developing a MAUI application in C# that implements a WebView2 and I need to change the style of the alert, prompt, and confirm dialogs. In WinUI applications everything is very modern, avoiding synchrony, but apparently only when it suits Microsoft, let me explain: If I do:

webView.CoreWebView2.Settings.AreDefaultScriptDialogsEnabled = false;
webView.CoreWebView2.ScriptDialogOpening += CustomScriptDialogOpening;

I can intercept the default JavaScript dialogs, but even if the CustomScriptDialogOpening function is async, the WebView doesn’t await it.

So,

How the heck can I make a prompt if I can’t await it, And there’s no way to launch the user prompt window synchronously, and if I launch it asynchronously, WebView2 doesn’t await it?

I don’t know if this has a solution… Thank you!

Steps to Reproduce

Create MAUI project with windows support. Add webview. Set AreDefaultScriptDialogsEnabled = false add Event on ScriptDialogOpening

Link to public reproduction project repository

https://github.com/MicrosoftEdge/WebView2Feedback

Version with bug

6.0.312

Last version that worked well

6.0.312

Affected platforms

Windows

Affected platform versions

Windows

Did you find any workaround?

No response

Relevant log output

No response

Issue Analytics

  • State:closed
  • Created 5 months ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
OMendozaGcommented, Apr 22, 2023

¡Ah, me gustaría presentarles a mi querido amigo GetDeferral()! Para WebView2 (¿y tal vez otras cosas en WinUI?) Si desea hacer cosas asíncronas en un controlador de eventos, debe hacer lo siguiente:

  1. Llame y guarde el resultado dee.GetDeferral()
  2. Haga su trabajo asíncrono (por ejemplo, usando C# async/await)
  3. Cuando haya terminado, invoque Complete()el objeto de aplazamiento que guardó

Vea un ejemplo aquí en el código BlazorWebView que se ejecuta en WinUI:

https://github.com/dotnet/maui/blob/2b823f8503748f115028e671b4fff9048628c462/src/BlazorWebView/src/Maui/Windows/WinUIWebViewManager.cs#L80-L166

(Está manejando un evento WebView2 diferente, pero el patrón es exactamente el mismo).

True…

My mistake due to ignorance.

I didn’t know this. It was so easy, and I didn’t know how to find the answer. Thank you Eilon. And thank you for the work you do with HybridWebview.

We had something similar in Xamarin, and your project encouraged us to switch to MAUI. 😃

Thanks again.

0reactions
Eiloncommented, Apr 23, 2023

You’re quite welcome. When I started using WebView2 it was the first time I saw this pattern as well, and was quite surprised by it!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Synchronous prompt on CustomScriptDialogOpening ...
NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop. - Synchronous prompt...
Read more >
Asynchronous script dialog prompts in WebView2 on ...
The solution is e.GetDeferral();. This will allow to continue doing work async while wait for response. Then you do e.
Read more >
Threading model for WebView2 apps
WebView2 relies on the message pump of the UI thread to run event handler callbacks and async method completion callbacks. If you use...
Read more >
Basic authentication for WebView2 apps
Basic authentication for WebView2 apps includes a sequence of authentication and navigation steps to retrieve a webpage from an HTTP server.
Read more >
Logging in to Box Sync with WebView2
Box Sync uses a modal dialog with a Microsoft Edge WebView2 component when logging into Box. This WebView component was updated in Box...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found