Disable WebView Scrolling in Windows Store Apps

In this article you will learn how to disable WebView scrolling in Windows Store Apps.

WebView is a control that enables developers to host HTML content. In the WinRT framework WebView still lacks some features compared to the WebBrowser of WPF. The WebView class inherits from the FrameworkElement class, but many properties of the base class does not work in WebView.
 
 Recently I was creating an app and I wanted to disable the horizontal and vertical scrolling of WebView. I tried the following code.

<webview x:name="webView" source="http://myawesomewebsite.com"
scrollviewer.verticalscrollbarvisibility="Disabled"
scrollviewer.verticalscrollmode="Disabled" />


But it didn't work. I was getting annoyed. I proceeded to use Bing and Google but didn't get any solution. WebView doesn't have its template also. Then I analyzed the control in detail. I finally understood that the scroll bars are due to HTML content, WebView itself doesn't have its own scroll bars. So I decided to go for a JavaScript solution. I applied some CSS properties in HTML content using JavaScript. It will display only that HTML content that is being displayed in the current viewport or say display area. WebView provides the InvokeScript method that executes the specified script function from the currently loaded HTML, with specific arguments. When WebView's LoadCompleted event occurs, I am invoking that JavaScript that disables the scrolling. Check out entire code given below.
  1. string DisableScrollingJs = @"function RemoveScrolling()  
  2.                               {  
  3.                                   var styleElement = document.createElement('style');  
  4.                                   var styleText = 'body, html { overflow: hidden; }'  
  5.                                   var headElements = document.getElementsByTagName('head');  
  6.                                   styleElement.type = 'text/css';  
  7.                                   if (headElements.length == 1)  
  8.                                   {  
  9.                                       headElements[0].appendChild(styleElement);  
  10.                                   }  
  11.                                   else if (document.head)  
  12.                                   {  
  13.                                       document.head.appendChild(styleElement);  
  14.                                   }  
  15.                                   if (styleElement.styleSheet)  
  16.                                   {  
  17.                                       styleElement.styleSheet.cssText = styleText;  
  18.                                   }  
  19.                               }";  
  20.    
  21. void webView_LoadCompleted(object sender, NavigationEventArgs e)  
  22. {  
  23.     webView.InvokeScript("eval"new[] { DisableScrollingJs });  
  24. }  
I hope this article will help you while developing a WebView oriented app if you need to implement such a feature.