Xamarin.Forms - WebView With ProgressBar

Introduction

This article demonstrates how to display web and HTML content in your app using WebView control in Xamarin.Forms application. The WebView displays the HTML content inside your app and shows how much content is loading in your HTML and web content of your app. This function is available in Xamarin.Forms.
 
Step 1

You can create a new Xamarin.Forms app by going to File >> New Project >>Visual C# >>Cross Platform >> Cross Platform App (Xamarin.Native or Xamarin.Forms ) and give the app name and solution name. Then, click OK.

 
Step 2

Now, select Blank App >> Platforms(Android, Universal Windows Platform, iOS) >> Xamarin.Forms >> .NET Standard and press OK.
 
 
 
Step 3

This opens the WebView and ProgressBar control to your MainPage.xaml page. For that, Solution Explorer >> WebViewApp(PCL) >>MainPage.xaml and double-click to open its design view. Here is the code.
 
MainPage.Xaml code
 
Used controls,
  • WebView
  • ProgressBar
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              xmlns:local="clr-namespace:WebViewApp"  
  5.              x:Class="WebViewApp.MainPage"  
  6.              Title="Xamarin.Forms App">  
  7.     <StackLayout>  
  8.         <Label Text="WebView with progess bar"  
  9.            FontSize="20"  
  10.            TextColor="BlueViolet"  
  11.            BackgroundColor="White"/>  
  12.         <ProgressBar Progress="0.2"  
  13.                  HorizontalOptions="FillAndExpand"  
  14.                  x:Name="progress"  
  15.                  IsVisible="True"/>  
  16.         <WebView x:Name="Webview"  
  17.              HeightRequest="1000"  
  18.              WidthRequest="1000"  
  19.              Navigating="OnNavigating"  
  20.              Navigated="OnNavigated"  
  21.              VerticalOptions="FillAndExpand"/>  
  22.   
  23.     </StackLayout>  
  24. </ContentPage>   

 
Step 4

After this, go to Solution Explorer >> WebViewApp(PCL) >> click open MainPage.xaml.cs page. Here is the code for this page.
 
MainPage.xaml.cs 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Xamarin.Forms;  
  7.   
  8. namespace WebViewApp  
  9. {  
  10.     public partial class MainPage : ContentPage  
  11.     {  
  12.         public MainPage()  
  13.         {  
  14.             InitializeComponent();  
  15.   
  16.             Webview.Source = "http://www.google.co.in";  
  17.         }  
  18.         protected async override void OnAppearing()  
  19.         {  
  20.             base.OnAppearing();  
  21.   
  22.             await progress.ProgressTo(0.9, 900, Easing.SpringIn);  
  23.         }  
  24.   
  25.         protected void OnNavigating(object sender, WebNavigatingEventArgs e)  
  26.         {  
  27.             progress.IsVisible = true;  
  28.         }  
  29.   
  30.         protected void OnNavigated(object sender, WebNavigatedEventArgs e)  
  31.         {  
  32.             progress.IsVisible = false;  
  33.         }  
  34.   
  35.     }  
  36. }  
 
 
Step 5

After completing your work with the design view, go to " Build " menu and click open "Configure Manager ". In the popup window, configure your startup projects. Click F5 or Run your project. Given below is the result.

 

Finally, we have successfully created a Xamarin.Forms WebView application.
  
Conclusion

I hope you learned how to use WebView control with ProgressBar in Xamarin.Forms using Visual Studio 15.5 Preview.

Up Next
    Ebook Download
    View all
    Learn
    View all