Added Iframe element, WebViewRenderer (basic implementation) and WebView sample
This commit is contained in:
parent
4f7dec9712
commit
078e62a6f4
|
@ -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))]
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -41,6 +41,7 @@ namespace Samples
|
|||
new TipCalcSample().Publish();
|
||||
new WeatherAppSample().Publish();
|
||||
new XuzzleSample().Publish();
|
||||
new WebViewSample().Publish();
|
||||
|
||||
UI.Present ("/display-alert");
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue