简体   繁体   中英

Turn off user interaction with a WebView in a UWP form

I am using a Windows.UI.Xaml.Controls.WebView to view some web content. I would like to turn off all user interaction with the component. No keyboard events, no mouse events. How can I achieve this?

I have tried with cancelling the GettingFocus event, but my test-javascript running inside the webview still receive mouse clicks.

Here is a slightly modified version of the code I have tested with. daWebView is a WebView component.

  public sealed partial class MainPage : Page
  {
    public MainPage()
    {
      this.InitializeComponent();

      this.RequiresPointer = RequiresPointer.Never;
      Application.Current.RequiresPointerMode = ApplicationRequiresPointerMode.WhenRequested;

      bool res = daWebView.Focus(FocusState.Unfocused);

      daWebView.GettingFocus += DaWebView_GettingFocus;
      daWebView.PointerMoved += DaWebView_PointerMoved;

      Uri uri = new Uri("ms-appx-web:///index.html");
      daWebView.Source = uri;
    }

    private void DaWebView_PointerMoved(object sender, PointerRoutedEventArgs e)
    {
      // never happens.
      Debug.WriteLine("pointer moved");
    }

    private void DaWebView_GettingFocus(UIElement sender, GettingFocusEventArgs args)
    {
      // not working, it still gets focus and receive mouse movement.
      args.Cancel = true;
    }
  }

Turn off user interaction with a WebView in a UWP form

There no such api could disable interaction with WebView directly, the better way is disable WebView content with js injection. You could use InvokeScriptAsync method to inject js method to disable interaction. For more please refer this document .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM