Reputation: 2768
Looking at this How to show loading image or progress bar on WebView it clearly says that the following approach should work but I am having an issue as I cannot access my ProgressBar
in public override void OnPageFinished(WebView view, string url)
My Code is the following
webView.Settings.JavaScriptEnabled = true;
webView.Settings.LoadWithOverviewMode = true;
webView.Settings.UseWideViewPort = true;
webView.SetWebViewClient(new WebViewClientClass());
webView.LoadDataWithBaseURL("file:///android_asset/", HTML_DATA, "text/html", "UTF-8", null);
Where my WebViewClientClass
is the following
#region Webview URL handler
internal class WebViewClientClass : WebViewClient
{
public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)
{
Android.Net.Uri url = request.Url;
view.LoadUrl(url.ToString());
return true;
}
public override void OnPageStarted(WebView view, string url, Bitmap favicon)
{
base.OnPageStarted(view, url, favicon);
Log.Info("101028", "loading started");
}
public override void OnPageFinished(WebView view, string url)
{
base.OnPageFinished(view, url);
Log.Info("101028", "loading finised");
PB.Visibility = ViewState.Gone; ***<<<<<<<------ ERROR HERE***
}
}
#endregion
I can see the results in logcat but when I try to access my ProgressBar
such as PB.Visibility = ViewState.Gone
in OnPageFinished
method - I get the following error
ERROR
An object reference is required for the non-static field, method, or property
PogressBar Code
private ProgressBar PB;
protected override async void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Read);
PB = FindViewById<ProgressBar>(Resource.Id.MyProgressBar);
}
Anyone knows what is the right approach for this? How do i make this work
Cheers
Upvotes: 2
Views: 143
Reputation: 1109
From your code it seems that your ProgressBar
belongs to your Activity
and you are trying to access it in WebViewClientClass
that's why your object reference is missing.
So you should have a method inside your activity class which will hide/show ProgressBar
and there must be some kind of call back mechanism to call this method
Define an interface like below
interface ProgressBarHandler{
public void hideProgress();
}
And in your activity class implement this interface
class MyActivity extends Activity implements ProgressBarHandler{
//other usual things in your activity
protected override async void OnCreate(Bundle savedInstanceState){
//here pass the activity instance to WebViewClientClass
webView.SetWebViewClient(new WebViewClientClass(this));
}
public void hideProgress(){
PB.Visibility = ViewState.Gone;
}
}
AND finally in your WebViewClientClass
class WebViewClientClass : WebViewClient
{
private ProgressBarHandler handler;
public WebViewClientClass(ProgressBarHandler handler){
this.handler = handler;
}
public override void OnPageFinished(WebView view, string url)
{
base.OnPageFinished(view, url);
Log.Info("101028", "loading finised");
handler.hideProgress();
}
}
Upvotes: 1