Default constructor not found of type 'MyClass'

I am trying to display a custom view from xamarin.forms. When I try to do this with the following code

[assembly: ExportRenderer (typeof (SwipeableCard), typeof (CardContainer))].

But the error is generated as

default constructor not found of type 'Com.andtinde.CardContainer'.

I think this is because the map container does not have a default constructor. And I cannot provide it because it inherits from Adapterview.

What am I doing wrong. Any help would be assigned.

+6
source share
4 answers

I think this is a known bug in Xamarin. However, I had the same problem with the dependency service. I solved this by installing β€œSDK Link Framework Only” in the iOS linker settings for the linker.

+21
source

You have already determined what you are doing wrong. ExportRenderer needs a default constructor to be able to do what it does. Why does inheriting from AdapterView prevent the creation of a default constructor?

You should be able to do something like:

public class CardContainer { public CardContainer() // This is a default constructor : base(...) // If base class requires arguments, pass them here { } } 
+3
source

Most likely, you should inherit from ViewRenderer and provide CardContainer as a native type of view. Then override the ViewRenderer OnElementChanged method and create a new CardContainer for the renderer. Below is a short version to get you started.

Registration:

 [assembly: ExportRenderer(typeof(SwipeableCard), typeof(SwipeableCardRenderer ))] 

Class definition:

  SwipeableCardRenderer : ViewRenderer<SwipeableCard, CardContainer> 

OnElementChanged override:

 if (this.Control == null) { this.SetNativeControl(new CardContainer(this.Context)); } 
+2
source

This is not a bug on Xamarin, you should know how to manage related assemblies. All runtime work in xamarin will get this error, for example, you cannot get the peroperty class at runtime or create an instance at runtime when this assembly is not connected. You can manage the related assemblies manually or install them in your application as Alessio Campanelli answer here if you install it manually, your application will get a large size in the release package, such as apk file. How to configure it manually?

  • Create an xml file in your android app name it "linked.xml"
  • Right-click and go to peroperties from the file "linked.xml" and set the "Build Action" to "LinkDescription"
  • add your assemblies that are used at runtime as follows:
  <?xml version="1.0" encoding="utf-8" ?> <linker> <assembly fullname="Newtonsoft.Json"> <type fullname="*" /> </assembly> </linker> 

Connector in xamarin

0
source

Source: https://habr.com/ru/post/983906/


All Articles