我该如何解决WebViewRenderer已过时

时间:2019-01-26 08:03:54

标签: xaml xamarin.forms

这就是我收到的错误消息,“ WebViewRenderer.WebViewRenderer()已过时

“此构造函数从2.5版开始已过时。请改用WebViewRenderer(Context)

{ test: /.*\.(gif|png|jpe?g|svg)$/, loader: "file-loader", options: { name: "./images/[name]_[hash:7].[ext]" }, exclude:  __dirname +  "/../src/assets/" },
{ test: /.*\.(ttf|eot|woff|woff2|svg)$/, loader: "file-loader", options: { name: "./assets/webfonts/[name].[ext]" }, exclude:  __dirname + "/../src/images/" }

我希望代码能够运行,因为我遵循了youtube上的特定系列,但事实并非如此。请问我做错了什么吗?

2 个答案:

答案 0 :(得分:3)

您需要添加上下文:

public MyWebViewRenderer(Context context) : base(context)
{
}

在您的代码中:

  namespace ZoomWebView.Droid
 {
    public class MyWebViewRenderer : WebViewRenderer
    {
        public MyWebViewRenderer(Context context) : base(context)
        {
        }


        protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
        {
            base.OnElementChanged(e);
            if (e.OldElement != null || Element == null)
            {
                return;
            }
            var element = Element as MyWebView;
            Control.Settings.TextZoom = element.ZoomInLevel;
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if(Control != null)
            {
                Control.Settings.BuiltInZoomControls = true;

                Control.Settings.DisplayZoomControls = true;
            }
            var element = Element as MyWebView;
            Control.Settings.TextZoom = element.ZoomInLevel;
            base.OnElementPropertyChanged(sender, e);
        }
    }
 }

答案 1 :(得分:0)

发生这种情况的原因是从v2.4开始的Xamarin.Forms非常简单,将Xamarin.Forms.Forms Context属性标记为过时。因此,要获取渲染器的全局上下文,有必要调用该渲染器的基类,即在构造函数中执行以下操作:

public MyWebViewRenderer(Context context) : base(context)
{

}

XF现在建议您使用James Montemagno的Current Activity Plugin。如果您需要当前上下文