Skip to content

Instantly share code, notes, and snippets.

@jameymcelveen
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jameymcelveen/d1c8b8aea28da53f6dd4 to your computer and use it in GitHub Desktop.
Save jameymcelveen/d1c8b8aea28da53f6dd4 to your computer and use it in GitHub Desktop.
Xamarin.Forms CALayerInvalidGeometry Exception workaround
//
// Add this file to your iOS project
//
using System;
using Xamarin.Forms;
namespace Common
{
public class JMWebPage : Page
{
//
// Static Fields
//
public static readonly BindableProperty SourceProperty;
//
// Properties
//
public WebViewSource Source
{
get
{
return (WebViewSource)base.GetValue(JMWebPage.SourceProperty);
}
set
{
base.SetValue(JMWebPage.SourceProperty, value);
}
}
static JMWebPage()
{
JMWebPage.SourceProperty = BindableProperty.Create("Source", typeof(WebViewSource), typeof(JMWebPage), null, BindingMode.OneWay, null, null, null, null);
}
}
}
//
// Add this file to your shared project
//
using MonoTouch.UIKit;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using Common;
using Common.iOS;
using MonoTouch.Foundation;
[assembly: ExportRenderer (typeof(JMWebPage), typeof(JMWebPageRenderer))]
namespace Common.iOS
{
public class JMWebPageRenderer : PageRenderer
{
protected UIWebView uiWebView;
public override void ViewDidLoad()
{
base.ViewDidLoad();
this.Element.PropertyChanged += new PropertyChangedEventHandler(this.OnHandlePropertyChanged);
uiWebView = new UIWebView();
uiWebView.ScrollView.DecelerationRate = UIScrollView.DecelerationRateNormal;
uiWebView.Frame = this.View.Bounds;
this.Add(uiWebView);
}
private void OnHandlePropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == JMWebPage.SourceProperty.PropertyName)
{
var source = ((JMWebPage)this.Element).Source;
var urlSource = source as UrlWebViewSource;
if (urlSource != null)
{
var url = NSUrl.FromString(urlSource.Url);
var request = NSUrlRequest.FromUrl(url);
uiWebView.LoadRequest(request);
}
else
{
var htmlSource = source as HtmlWebViewSource;
if (htmlSource != null)
{
NSUrl url = string.IsNullOrWhiteSpace(htmlSource.BaseUrl) ? null : new NSUrl(htmlSource.BaseUrl);
uiWebView.LoadHtmlString(htmlSource.Html, url);
}
}
}
}
}
}
//
// Add this file to your shared project
// create a UrlWebPage with:
// var page = new UrlWebPage { Url = "http://google.com" };
//
using System;
using Xamarin.Forms;
namespace Common
{
#if __IOS2__
public class UrlWebPage : UrlWebPage_iOS { }
#else
public class UrlWebPage : UrlWebPageBase { }
#endif
public class UrlWebPageBase : ContentPage
{
private WebView webView;
public string Url { get; set; }
public UrlWebPageBase()
{
webView = new WebView();
Content = new StackLayout {
Children = { webView }
};
}
protected override void OnAppearing()
{
base.OnAppearing();
HeightRequest = this.Height;
WidthRequest = this.Width;
webView.Source = new UrlWebViewSource { Url = this.Url };
}
}
public class UrlWebPage_iOS : JMWebPage
{
public string Url { get; set; }
protected override void OnAppearing()
{
base.OnAppearing();
this.Source = new UrlWebViewSource { Url = this.Url };
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment