Merge pull request #70 from jsuarezruiz/master

Iframe Element, WebViewRenderer (basic) and Sample
This commit is contained in:
Frank A. Krueger 2018-02-01 13:41:28 -08:00 committed by GitHub
commit af2b025ac3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 173 additions and 0 deletions

View File

@ -22,6 +22,7 @@ using Xamarin.Forms;
[assembly: ExportRenderer(typeof(Slider), typeof(SliderRenderer))]
[assembly: ExportRenderer (typeof (Switch), typeof (SwitchRenderer))]
[assembly: ExportRenderer (typeof (TimePicker), typeof (TimePickerRenderer))]
[assembly: ExportRenderer(typeof(WebView), typeof(WebViewRenderer))]
[assembly: ExportImageSourceHandler (typeof (FileImageSource), typeof (FileImageSourceHandler))]
[assembly: ExportImageSourceHandler (typeof (StreamImageSource), typeof (StreamImagesourceHandler))]
[assembly: ExportImageSourceHandler (typeof (UriImageSource), typeof (ImageLoaderSourceHandler))]

View File

@ -0,0 +1,112 @@
using System;
using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Internals;
namespace Ooui.Forms.Renderers
{
public class WebViewRenderer : ViewRenderer<WebView, Div>, IWebViewDelegate
{
private bool _disposed;
private Iframe _iframe;
void IWebViewDelegate.LoadHtml(string html, string baseUrl)
{
try
{
if (string.IsNullOrEmpty(html))
{
if (Element.Source is HtmlWebViewSource urlWebViewSource)
{
html = urlWebViewSource.Html;
}
}
if (_iframe != null)
{
_iframe.Src = html;
}
}
catch (Exception ex)
{
Log.Warning("WebView load string", $"WebView load string failed: {ex}");
}
}
void IWebViewDelegate.LoadUrl(string url)
{
try
{
if (string.IsNullOrEmpty(url))
{
if (Element.Source is UrlWebViewSource urlWebViewSource)
{
url = urlWebViewSource.Url;
}
}
if (_iframe != null)
{
_iframe.Src = url;
}
}
catch (Exception ex)
{
Log.Warning("WebView load url", $"WebView load url failed: {ex}");
}
}
public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint)
{
var size = new Size(100, 100);
return new SizeRequest(size, size);
}
protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
if (Control == null)
{
var embed = new Div { ClassName = "embed-responsive" };
_iframe = new Iframe();
embed.AppendChild(_iframe);
SetNativeControl(embed);
}
}
Load();
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == WebView.SourceProperty.PropertyName)
Load();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing && !_disposed)
{
if (_iframe != null)
{
_iframe = null;
}
_disposed = true;
}
}
private void Load()
{
Element?.Source?.Load(this);
}
}
}

18
Ooui/Iframe.cs Normal file
View File

@ -0,0 +1,18 @@
namespace Ooui
{
public class Iframe : Element
{
public Iframe()
: base("iframe")
{
}
string src = null;
public string Src
{
get => src;
set => SetProperty(ref src, value, "src");
}
}
}

View File

@ -41,6 +41,7 @@ namespace Samples
new TipCalcSample().Publish();
new WeatherAppSample().Publish();
new XuzzleSample().Publish();
new WebViewSample().Publish();
UI.Present ("/display-alert");

41
Samples/WebViewSample.cs Normal file
View File

@ -0,0 +1,41 @@
using Ooui;
using Xamarin.Forms;
namespace Samples
{
public class WebViewSample : ISample
{
public string Title => "Xamarin.Forms WebView Sample";
public Ooui.Element CreateElement()
{
var panel = new StackLayout();
var titleLabel = new Xamarin.Forms.Label
{
Text = "WebView",
FontSize = 24,
FontAttributes = FontAttributes.Bold
};
panel.Children.Add(titleLabel);
WebView webview = new WebView
{
Source = "http://www.xamarin.com"
};
panel.Children.Add(webview);
var page = new ContentPage
{
Content = panel
};
return page.GetOouiElement();
}
public void Publish()
{
UI.Publish("/webview", CreateElement);
}
}
}