我一直试图弄清楚下次更改BindableProperty时如何调用自定义渲染器OnElementChanged。
在我的情况下,为了理解目的,我需要刷新android,uwp,ios渲染器中的webview网址。
首次初始化值有效,但稍后当URL更改时,它不会调用。
如果在自定义渲染器类中有一个函数,那么我们可以调用并更新本机控件。
尝试使用android和uwp,但有同样的问题。一旦url加载进一步更改,它就不会刷新webview。
所以为了示例,我提供了UWP渲染器,如果它在这里工作,那么类似的更改可以在android渲染器中完成。但不确定在哪里做出改变才能使其发挥作用。
XAML
<StackLayout>
<Entry Keyboard="Url" x:Name="txtUrl" Placeholder="Enter the URL" />
<Button x:Name="ButtonImage" Text="Search" Clicked="OnButtonClicked" />
<local:HybridWebView x:Name="webView" />
</StackLayout>
Xaml.cs
public partial class WebVieDemo : ContentPage
{
public WebVieDemo()
{
InitializeComponent();
webView.Uri = "https://www.101cookbooks.com/archives/blueberry-beet-pancakes-vegan-recipe.html";
}
private void OnButtonClicked(object sender, EventArgs e)
{
txtUrl.Text = "https://stackoverflow.com/questions/1531093/how-do-i-get-the-current-date-in-javascript";
webView.Uri = txtUrl.Text; // doesn't refresh the webview
}
}
HybridWebView.cs
public class HybridWebView : WebView
{
public static readonly BindableProperty UriProperty = BindableProperty.Create(
propertyName: "Uri",
returnType: typeof(string),
declaringType: typeof(HybridWebView),
defaultValue: default(string));
public string Uri
{
get { return (string)GetValue(UriProperty); }
set { SetValue(UriProperty, value); }
}
public static readonly BindableProperty HtmlSourceProperty = BindableProperty.Create(
propertyName: "HtmlSource",
returnType: typeof(string),
declaringType: typeof(HybridWebView),
defaultValue: default(string));
public string HtmlSource
{
get { return (string)GetValue(HtmlSourceProperty); }
set { SetValue(HtmlSourceProperty, value); }
}
}
UWP渲染器
[assembly: ExportRenderer(typeof(HybridWebView), typeof(WindowsWebViewRenderer))]
namespace DemoApp.UWP
{
public class WindowsWebViewRenderer : ViewRenderer<HybridWebView, WebView>
{
HybridWebView hybridWebView = null;
protected override void OnElementChanged(ElementChangedEventArgs<HybridWebView> e)
{
base.OnElementChanged(e);
if (Control == null)
{
// Create the native control and use SetNativeControl
var webView = new WebView();
webView.LoadCompleted += WebView_LoadCompleted;
SetNativeControl(webView);
}
if (e.OldElement != null)
{
// Cleanup resources and remove event handlers for this element.
}
if (e.NewElement != null)
{
// Use the properties of this element to assign to the native control, which is assigned to the base.Control property
hybridWebView = e.NewElement;
if (hybridWebView.Uri != null)
Control.Source = new Uri(e.NewElement.Uri);
}
}
async private void WebView_LoadCompleted(object sender, Windows.UI.Xaml.Navigation.NavigationEventArgs e)
{
hybridWebView.HtmlSource = await Control.InvokeScriptAsync("eval", new string[] { "document.documentElement.outerHTML;" });
}
}
}
答案 0 :(得分:3)
在自定义渲染器中,您需要为此重写OnElementPropertyChanged,而不是OnElementChanged,如下所示:
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == HybridWebView.UriProperty.PropertyName)
{
if (Control.Uri != null)
Control.Source = new Uri(Control.Uri);
}
}