Disable Back / forward swipe navigation in WebView

Morten Tvedten 20 Reputation points
2024-02-18T10:54:16.82+00:00

I am making a kiosk type app for Android and Windows with touch screens that will host a dedicated web application inside a Webview. It runs full screen. However I need to disable any kind of navigation and context menus that the webview can do. I need to disable the swipe to go back or forward, which is a function coming from Edge. ( I tried to disable it in Edge in my computer, but this does not affect the Webview). I also need to disable the right-click context menu from Edge in the Webview. How can I achieve this?

Developer technologies .NET .NET MAUI
Developer technologies .NET Other
{count} votes

Accepted answer
  1. Anonymous
    2024-02-19T05:29:40.1033333+00:00

    Hello,

    You can disable right-click context menu by coreWebView2.Settings.AreDefaultContextMenusEnabled = false; and disable the swipe navigation by coreWebView2.Settings.IsSwipeNavigationEnabled = false;

    Firstly, please add x:Name for your webview. Then you can get the webview object in the background code and implement the Navigation event. Please use Conditional compilation to wrap this event as well.

    #if WINDOWS
                Mywebview.Navigating += Mywebview_Navigating;
    #endif
    

    Then, you can disable the right-click context menu and the swipe navigation like following code.

     private void Mywebview_Navigating(object? sender, WebNavigatingEventArgs e)
            {
    #if WINDOWS
                WebView webview = (WebView)sender;
                Microsoft.Web.WebView2.Core.CoreWebView2 coreWebView2 = (webview.Handler.PlatformView as Microsoft.UI.Xaml.Controls.WebView2).CoreWebView2;
                coreWebView2.Settings.IsSwipeNavigationEnabled = false;
                coreWebView2.Settings.AreDefaultContextMenusEnabled = false;    
                coreWebView2.Settings.AreBrowserAcceleratorKeysEnabled = false;
                webview.Navigating -= Mywebview_Navigating;
    #endif
            }
    

    Best Regards, Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.