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

Snorvarg 6 Reputation points
2019-12-06T12:33:36.533+00:00

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?

Universal Windows Platform (UWP)
{count} vote

2 answers

Sort by: Most helpful
  1. macintoshpro 36 Reputation points
    2020-01-08T04:39:15.62+00:00

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

    2 people found this answer helpful.
    0 comments No comments

  2. Richard Zhang-MSFT 6,936 Reputation points
    2019-12-09T05:54:26.557+00:00

    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.

    1 person found this answer helpful.
    0 comments No comments