Search code examples
windows-phone-8webviewwindows-phone-8.1windows-phone

How to add progress bar or progress ring for webview project for windows mobile app


I have created a Webview Project for windows phone app to load google.com. It's working fine but I'm unable to add a Progress bar or Progress Ring. Can any one please help me?

namespace App2
{
   public sealed partial class MainPage : Page
   {

      private static readonly Uri HomeUri = new Uri("http://www.google.com", UriKind.Absolute);

      public MainPage()
      {
         this.InitializeComponent();
         this.NavigationCacheMode = NavigationCacheMode.Required;
      }

      protected override void OnNavigatedTo(NavigationEventArgs e)
      {
         WebViewControl.Navigate(HomeUri);
         HardwareButtons.BackPressed += this.MainPage_BackPressed;
      }

      protected override void OnNavigatedFrom(NavigationEventArgs e)
      {
         HardwareButtons.BackPressed -= this.MainPage_BackPressed;
      }

      private void MainPage_BackPressed(object sender, BackPressedEventArgs e)
      {
         if (WebViewControl.CanGoBack)
         {
            WebViewControl.GoBack();
            e.Handled = true;
         }
      }

      private void Browser_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
      {
         if (!args.IsSuccess)
         {
            Debug.WriteLine("Navigation to this page failed, check your internet connection.");
         }
      }
   }
}

Solution

  • In your XAML add a progress ring over the webview so that it overlaps webview for example

    <Grid>
         <Grid x:Name="webViewHolder" >
                        <WebView x:Name="wvPage" Loaded="WebView_Loaded" NavigationCompleted="WebView_NavigationCompleted" NavigationStarting="wvPage_NavigationStarting"></WebView>
                    </Grid>
    
                    <ProgressRing  x:Name="myProgressRing" IsActive="True" Height="90" Width="90" Background="Transparent" Foreground="#EF4D17"/>
    </Grid>
    

    Now in code Behind

    private void wvPage_NavigationStarting(Windows.UI.Xaml.Controls.WebView sender, WebViewNavigationStartingEventArgs args)
            {            
                myProgressRing.IsActive = true;
            }
    .
    .
    .
     private void WebView_NavigationCompleted(Windows.UI.Xaml.Controls.WebView sender, WebViewNavigationCompletedEventArgs args)
            {
                myProgressRing.IsActive = false;
            }