Created
February 25, 2018 13:27
-
-
Save yuv4ik/a737d3cba45c170626f1ec78bcabb525 to your computer and use it in GitHub Desktop.
Xamarin.Forms WebView with ProgressBar for iOS and Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Android.Content; | |
using WebViewWithProgressBar.Droid; | |
using Xamarin.Forms; | |
using Xamarin.Forms.Platform.Android; | |
// https://stackoverflow.com/a/8467430/1970317 | |
[assembly: ExportRenderer(typeof(WebView), typeof(GenericWebViewRenderer))] | |
namespace WebViewWithProgressBar.Droid | |
{ | |
public class GenericWebViewRenderer : WebViewRenderer | |
{ | |
Context ctx; | |
public GenericWebViewRenderer(Context context) : base(context) | |
{ | |
ctx = context; | |
} | |
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e) | |
{ | |
base.OnElementChanged(e); | |
if (Control == null) | |
return; | |
var progressBar = new Android.Widget.ProgressBar(ctx, null, Android.Resource.Attribute.ProgressBarStyleHorizontal); | |
Control.SetWebChromeClient(new MyWebChromeClient(progressBar)); | |
Control.AddView(progressBar); | |
} | |
class MyWebChromeClient : Android.Webkit.WebChromeClient | |
{ | |
Android.Widget.ProgressBar progressBar; | |
public MyWebChromeClient(Android.Widget.ProgressBar progressBar) | |
{ | |
this.progressBar = progressBar; | |
} | |
public override void OnProgressChanged(Android.Webkit.WebView view, int newProgress) | |
{ | |
progressBar.SetProgress(newProgress, true); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Foundation; | |
using UIKit; | |
using WebViewWithProgressBar.iOS; | |
using Xamarin.Forms; | |
using Xamarin.Forms.Platform.iOS; | |
// https://stackoverflow.com/questions/21263358/uiwebview-with-progress-bar | |
[assembly: ExportRenderer(typeof(WebView), typeof(GenericWebViewRenderer))] | |
namespace WebViewWithProgressBar.iOS | |
{ | |
public class GenericWebViewRenderer : ViewRenderer<WebView, UIWebView> | |
{ | |
protected override void OnElementChanged(ElementChangedEventArgs<WebView> e) | |
{ | |
base.OnElementChanged(e); | |
if (Control == null) | |
{ | |
var progressBar = new UIProgressView(UIProgressViewStyle.Bar); | |
progressBar.TintColor = UIColor.Green; | |
progressBar.TrackTintColor = UIColor.Black; | |
progressBar.ProgressTintColor = UIColor.Red; | |
var webView = new UIWebView(Frame); | |
webView.AddSubview(progressBar); | |
SetNativeControl(webView); | |
Control.Delegate = new MyUIWebViewDelegate(progressBar); | |
webView.LoadRequest(new NSUrlRequest(new NSUrl("https://google.com"))); | |
} | |
} | |
class MyUIWebViewDelegate : UIWebViewDelegate | |
{ | |
UIProgressView progressBar { get; } | |
public MyUIWebViewDelegate(UIProgressView progressBar) | |
{ | |
this.progressBar = progressBar; | |
} | |
public override void LoadStarted(UIWebView webView) | |
{ | |
progressBar.SetProgress(0.1f, false); | |
} | |
public override void LoadingFinished(UIWebView webView) | |
{ | |
progressBar.SetProgress(1.0f, true); | |
} | |
public override void LoadFailed(UIWebView webView, NSError error) | |
{ | |
// TODO: | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment