You_Should_Not_Call_base_In_This_Method 跨界Bug 之 Xamarin Forms Renderer

最近在做Xamarin Forms的项目 , 关于Xamarin相信大家比较陌生 ,其实它也是一个跨平台的技术,可以参考下面这个官方文档去了解。

What is Xamarin.Forms?

Xamarin.Forms is an open-source UI framework. Xamarin.Forms allows developers to build Android, iOS, and Windows applications from a single shared codebase.

Xamarin.Forms allows developers to create user interfaces in XAML with code-behind in C#. These interfaces are rendered as performant native controls on each platform.

好了,介绍不多说,总而言之,它就是一个跨平台的技术。

今天要讲的Bug,是在项目开发的时候需要自定义一个WKWebView在iOS中,你可能会疑问为什么怎么在iOS中自定义WKWebView。你可以先看下下面这个文档的介绍:

Customizing a WebView

A Xamarin.Forms WebView is a view that displays web and HTML content in your app. This article explains how to create a custom renderer that extends the WebView to allow C# code to be invoked from JavaScript.

不用怀疑,没错就是Forms不能实现的界面需要通过Renderer的方式调用iOS原生的方法,类似于用C#写原生。如果你了解更多关于Forms,很多场景下都需要用到Renderer来写界面。

好了,此处我贴的是Custom WebView的文档,那么我的问题也是在实现这个功能的时候出现的,这个Bug如下:

**Foundation.You_Should_Not_Call_base_In_This_Method:** 'Exception of type 'Foundation.You_Should_Not_Call_base_In_This_Method' was thrown.'

同时Visual Studio的界面变成了下面这个样:

当你看到这个时候,很懵,因为没有控制台的log显示哪里的问题。但是回忆一下,发生这个Bug之前我就写了几行代码,就这样了。

于是,Check了写的代码,在WKWebView里用到了NavigationDelegate,就是加了它才出现的。于是锁定问题代码如下:

 public class NavigationDelegat : WKNavigationDelegate
    {
        NSMutableArray multiCookieArr = new NSMutableArray();
        public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
        {
            string fontSize = "250%"; // 250% is the size of font
            string stringsss = String.Format(@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '{0}'", fontSize);
            WKJavascriptEvaluationResult handler = (NSObject result, NSError err) =>
            {
                if (err != null)
                {
                    System.Console.WriteLine(err);
                }
                if (result != null)
                {
                    System.Console.WriteLine(result);
                }
            };
            webView.EvaluateJavaScript(stringsss, handler);
            base.DidFinishNavigation(webView, navigation);
        }
    }

于是根据Error的提示 You_Should_Not_Call_base_In_This_Method , 这下你知道了原因了吧。方法中多了

base.DidFinishNavigation(webView, navigation);

把这个方法去掉就可以了。

但是,我们不是一下子就能看懂这个Error的提示的,所以你可以在Google上搜索一下这个Error,发现了官方的Error的解释如下:

 

You_Should_Not_Call_base_In_This_Method Class

This class exits purely as a warning to future generations.   You called a method using “base”, but this was not required.

所以,最终这个Bug就这样解决了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值