question

Snorvarg-8336 avatar image
Snorvarg-8336 asked ·

How to disable user interaction with a WebView (Windows.UI.Xaml.Controls)

Hi,

I would like to know how to disable all user interaction with a WebView component in a UWP project.

I have tried to cancel the GettingFocus event, to no avail.

It should also disable the user interaction when running on an xbox.

How can I disable all user interaction, keyboard, mouse, xbox hand controller?

windows-uwpwindows-uwp-xaml
2 comments
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, Sorry to bother you, did my reply solve your problem? Do you have any questions about this issue?

0 Votes 0 · ·

Hello, is there any new information in your question, and can we provide you with new help?

0 Votes 0 · ·
RichardZhang-MSFT avatar image
RichardZhang-MSFT answered ·

Hello,​

Welcome to our Microsoft Q&A platform!

In WebView, there is no API to disable user-web page interaction.

But we can inject Javascript to add a layer of invisible mask to Html document to prevent user interaction.

We can listen to the WebView.LoadCompleted event and perform the following processing:

 private async void WebView_LoadCompleted(object sender, NavigationEventArgs e)
 {
     var webView = sender as WebView;
    
     var height = await webView.InvokeScriptAsync("eval", new string[] { "document.body.scrollHeight.toString()" });
    
     string script = "var body=document.body;" +
         "var temp = document.createElement('div'); " +
         $"temp.style=\"height:{height}px; width:100%; background:transparent;position:absolute;top:0;left:0;z-index:9999;\";" +
         "body.appendChild(temp)";
    
     await webView.InvokeScriptAsync("eval", new string[]
     {
         script
     });
 }

Thanks.


Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

macintoshpro avatar image
macintoshpro answered ·

My solution, put a grid with almost transparent background after WebView
code




Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.