Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Xamarin Forms custom webView renderer with activity load
***
In Shared code:
public class WebViewPage : ContentPage
{
}
...
await Navigation.PushAsync (new WebViewPage());
***
using System;
using ViewRSS;
using Xamarin.Forms;
using ViewRSS.iOS;
using Xamarin.Forms.Platform.iOS;
using Foundation;
using UIKit;
using CoreGraphics;
using System.Threading;
using System.Threading.Tasks;
[assembly: ExportRenderer (typeof (WebViewPage), typeof (WebViewPageRenderer))]
namespace ViewRSS.iOS
{
public class WebViewPageRenderer : PageRenderer
{
UIWebView webView;
LoadingOverlay loadingOverlay;
public WebViewPageRenderer ()
{
}
public override void ViewDidAppear (bool animated)
{
base.ViewDidAppear (animated);
App.Debug ("ViewDidAppear");
var url = new NSUrlRequest (new NSUrl (ItemsPage._item.Link), NSUrlRequestCachePolicy.UseProtocolCachePolicy, 15.0);
webView.LoadRequest (url);
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
App.Debug ("ViewDidLoad");
webView = new UIWebView (View.Bounds);
webView.WeakDelegate = this;
webView.ScalesPageToFit = true;
View.AddSubview(webView);
bool didStart = false;
bool didFinish = false;
loadingOverlay = new LoadingOverlay(webView.Bounds);
webView.Add(loadingOverlay);
UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true;
//When the web view is finished loading
webView.LoadFinished += (object sender, EventArgs e) => {
if (didFinish == false)
{
loadingOverlay.Hide();
UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false;
didStart = false;
didFinish = true;
}
};
//If there is a load error
webView.LoadError += (object sender, UIWebErrorArgs e) => {
if (didFinish == false)
{
loadingOverlay.Hide();
UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false;
didStart = false;
didFinish = true;
}
};
}
}
public class LoadingOverlay : UIView {
// control declarations
UIActivityIndicatorView activitySpinner;
UILabel loadingLabel;
public LoadingOverlay (CGRect frame) : base (frame)
{
// configurable bits
BackgroundColor = UIColor.Black;
Alpha = 0.75f;
AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;
nfloat labelHeight = 22;
nfloat labelWidth = Frame.Width - 20;
// derive the center x and y
nfloat centerX = Frame.Width / 2;
nfloat centerY = Frame.Height / 2;
// create the activity spinner, center it horizontall and put it 5 points above center x
activitySpinner = new UIActivityIndicatorView(UIActivityIndicatorViewStyle.WhiteLarge);
activitySpinner.Frame = new CGRect (
centerX - (activitySpinner.Frame.Width / 2) ,
centerY - activitySpinner.Frame.Height - 20 ,
activitySpinner.Frame.Width ,
activitySpinner.Frame.Height);
activitySpinner.AutoresizingMask = UIViewAutoresizing.FlexibleMargins;
AddSubview (activitySpinner);
activitySpinner.StartAnimating ();
// create and configure the "Loading Data" label
loadingLabel = new UILabel(new CGRect (
centerX - (labelWidth / 2),
centerY + 20 ,
labelWidth ,
labelHeight
));
loadingLabel.BackgroundColor = UIColor.Clear;
loadingLabel.TextColor = UIColor.White;
loadingLabel.Text = "Loading Data...";
loadingLabel.TextAlignment = UITextAlignment.Center;
loadingLabel.AutoresizingMask = UIViewAutoresizing.FlexibleMargins;
AddSubview (loadingLabel);
}
/// <summary>
/// Fades out the control and then removes it from the super view
/// </summary>
public void Hide ()
{
UIView.Animate (
0.5, // duration
() => { Alpha = 0; },
() => { RemoveFromSuperview(); }
);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.