因此,我在我的Xamarin Forms应用程序中有一个WebView,但是我想通过创建一个自定义的呈现程序来更改我的iOS WebView,如下所推荐的:https://forums.xamarin.com/discussion/129626/how-does-one-implement-wkwebview-ios-in-a-cross-platform-application
不过,我不确定如何修改我的XAML文件,以调用iOS的自定义渲染器和Android的普通WebView。目前,我的XAML文件如下所示:
<WebView Source="{Binding StartUrl}"
Navigated="OnBrowserNavigated"
Navigating="OnBrowserNavigating"
AbsoluteLayout.LayoutFlags="All"
AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1"/>它使用关键字WebView (Xamarin.Forms.WebView)。但如果我使用此方法为iOS创建自定义渲染器:
// Create a new class:
public class MyWebView : WebView
{
public static readonly BindableProperty UrlProperty = BindableProperty.Create(...);
...
}
// Create custom renderer:
[assembly: ExportRenderer(typeof(MyWebView), typeof(MyWebViewRenderer))]
namespace WKWebViewDemo.iOS
{
...
}我该如何像页面上说的那样调用这个渲染器呢
<StackLayout>
<local:MyWebView Url="https://www.microsoft.com" VerticalOptions="FillAndExpand"/>
</StackLayout>仍然像我的XAML页面那样调用android的Xamarin.Forms.WebView吗?
发布于 2020-02-10 11:26:09
因为Url属性是您在MyWebView中定义的,所以如果您删除MyWebView类,它将抛出not defined错误。
如果你想同时在安卓和ios中使用Source属性,你不能定义一个自定义的webview,直接在ios中使用渲染器中的WebView,就像这样:
// Create custom renderer:
[assembly: ExportRenderer(typeof(WebView), typeof(MyWebViewRenderer))]
namespace WKWebViewDemo.iOS
{
public class MyWebViewRenderer : ViewRenderer<WebView, WKWebView>
{
WKWebView _wkWebView;
protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
{
base.OnElementChanged(e);
if (Control == null)
{
var config = new WKWebViewConfiguration();
_wkWebView = new WKWebView(Frame, config);
SetNativeControl(_wkWebView);
}
if (e.NewElement != null)
{
Control.LoadRequest(new NSUrlRequest(new NSUrl(((UrlWebViewSource)Element.Source).Url)));
}
}
}
}发布于 2020-02-08 05:23:36
<WebView Source="{Binding StartUrl}"
Url="{Binding StartUrl}"
...>所以这确实起作用了,iOS WebView运行得很好,但是我想要删除网址并使用“源”的方式是这样的。
namespace Apm.Mobile.Core.Controls
{
public class BetterWebView : WebView
{
public static readonly BindableProperty UrlProperty = BindableProperty.Create(
propertyName: "Source", // This is now source instead of Url
returnType: typeof(string),
declaringType: typeof(BetterWebView),
defaultValue: default(string));
public new string Source // This string is now Source instead of Url
{
get { return (string)GetValue(UrlProperty); }
set { SetValue(UrlProperty, value); }
}
}
}https://stackoverflow.com/questions/60120697
复制相似问题