You should not call base in this method Xamarin Forms Renderer

Posted by jeremuck on Wed, 15 Jan 2020 10:25:47 +0100

Recently, I am working on Xamarin Forms project. I believe you are unfamiliar with Xamarin. In fact, it is also a cross platform technology. Please refer to the following official document to understand.

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.

Well, I won't say much about it. In a word, it's a cross platform technology.

Today's Bug is that you need to customize a WKWebView in iOS during project development. You may wonder why you need to customize WKWebView in iOS. You can take a look at the introduction of the following document:

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.

No doubt, it's true that the interface that Forms can't implement needs to call iOS native methods through Renderer, which is similar to writing native methods with C ා. If you know more about Forms, you need to use Renderer to write the interface in many scenes.

OK, here I post the document of Custom WebView, so my problem also occurs when realizing this function. The Bug is as follows:

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

At the same time, the interface of Visual Studio becomes the following:

When you see this, you are very confused, because there is no console log to show where the problem is. But remember, I wrote a few lines of code before this Bug happened, that's all.

Therefore, Check the code written and use NavigationDelegate in WKWebView, which is the result of adding it. The locking problem code is as follows:

 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);
        }
    }

So you should not call base in this method according to the prompt of Error. Now you know the reason. There are many ways

base.DidFinishNavigation(webView, navigation);

Just remove this method.

However, we can't understand the Error prompt in a short time, so you can search the Error on Google and find the official Error explanation as follows:

 

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.

So, in the end, this Bug was solved.

Published 7 original articles, won praise 2, visited 20000+
Private letter follow

Topics: iOS Android Windows Javascript