How to get the progress of WebView while loading the data, Xamarin.Forms

I am developing an App using Xamarin.Forms for listing the news from different sources. I use a webView to open the link corresponding to the news. But I want to show the progress while loading the webpage into web view, like the progress bar on Safari App. For this I have used the ProgressBar element like this:

<StackLayout>
            <!-- WebView needs to be given height and width request within layouts to render. -->

            <ProgressBar Progress ="" HorizontalOptions="FillAndExpand" x:Name="progress"/>
            <WebView x:Name="webView" 
                     HeightRequest="1000"
                     WidthRequest="1000"  
                     VerticalOptions= "FillAndExpand" 
                     Navigating="webOnNavigating"
                     Navigated="webOnEndNavigating"/>
        </StackLayout>

and in the code I have used

void webOnNavigating (object sender, WebNavigatingEventArgs e)
        {
            progress.IsVisible = true;

        }

        void webOnEndNavigating (object sender, WebNavigatedEventArgs e)
        {
            progress.IsVisible = false;
        }

But I want to show also the progress of loading the data, not just an indication that is loading and load. I want the user to know that the data are loading. Is there a way to achieve this.

Answers

  • AdamPAdamP AUUniversity ✭✭✭✭✭

    @xhuliomitre.2420 - I don't know of anything that can do this and if you were to code it yourself it would take some effort. That small progress bar would have a lot of stuff going on in the background.

    You would need to get the html, find all the resources, download the resources (account for the fact style sheets may reference more resources). Calculate the total bytes to download and how many have been downloaded. If you wanted to see any time remaining that would be even harder.

    I think your users will appreciate just a activity indicator and they will see the page load as it get rendered.

    Unless you want to download the html (with no resources, such as images etc) then assign it to the WebView, you could progress bar a download via the HttpClient. But again, that is with html only, no images or stylesheets and there will still be an additional delay in the WebView doing a render (not big but possibly noticeable).

    I also think that Safari probably cheats in its progress bar and estimates a lot of it. There is no way it could with 100% certainty know what there is to download until its all downloaded.

  • SamirgcSamirgc USMember ✭✭
    edited February 2017

    I think this solution will help everyone visually for implementation:

Sign In or Register to comment.