简体   繁体   English

未找到“MyClass”类型的默认构造函数

[英]default constructor not found of type'MyClass'

I am trying to render a custom view from from xamarin.forms.我正在尝试从 xamarin.forms 呈现自定义视图。 When I try to render it from with following code当我尝试使用以下代码呈现它时

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

But an error throws like但是会抛出一个错误,例如

default constructor not found of type 'Com.andtinde.CardContainer' .未找到类型为 'Com.andtinde.CardContainer' 的默认构造函数。

I guess it is because the card container does not have a default constructor.我猜是因为卡片容器没有默认构造函数。 And I cannot provide one because it inherits from Adapterview.我不能提供一个,因为它继承自 Adapterview。

What I am doing wrong.我做错了什么。 Any help would be appriciated.任何帮助将被appriciated。

I think this is a known bug in Xamarin.我认为这是 Xamarin 中的一个已知错误。 I had a same problem with a Dependency Service though.不过,我在依赖服务方面遇到了同样的问题。 I solved it by setting "Link Framework SDKs Only" in Linker behavior of iOS Build Options.我通过在 iOS 构建选项的链接器行为中设置“仅链接框架 SDK”来解决它。

You've already identified what you're doing wrong.你已经确定你做错了什么。 The ExportRenderer needs a default constructor to be able to do what it's doing. ExportRenderer需要一个默认构造函数才能完成它正在做的事情。 Why does inheriting from AdapterView prevent you from having a default constructor?为什么从 AdapterView 继承会阻止您拥有默认构造函数?

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
    {
    }
}

This is not a bug on Xamarin, you must know about how to manage your linked assemblies.all of runtime works in xamarin will get error like this for example you cannot get peroperty of class in runtime or create instance in runtime when that assembly is not linked.这不是 Xamarin 上的错误,您必须知道如何管理链接的程序集。 xamarin 中的所有运行时工作都会出现这样的错误,例如,当该程序集不是时,您无法在运行时获取类的 peroperty 或在运行时创建实例链接。 You can manage linked assemblies manualy or set those in your application option as Alessio Campanelli answer here , if you don't set it manualy your application will get large size in release package like apk file.您可以手动管理链接的程序集或在您的应用程序选项中设置它们,如Alessio Campanelli在此处回答,如果您不手动设置它,您的应用程序将在像 apk 文件这样的发布包中变大。 How to set it manualy?怎么手动设置?

  1. Create a xml file in your android application name it "linked.xml"在您的 android 应用程序中创建一个 xml 文件,将其命名为“linked.xml”
  2. Right click and go to peroperties of "linked.xml" and set "Build Action" to "LinkDescription"右键单击并转到“linked.xml”的peroperties并将“Build Action”设置为“LinkDescription”
  3. add your assemblies those are used on runtime like this:添加在运行时使用的程序集,如下所示:
    <?xml version="1.0" encoding="utf-8" ?>
    <linker>
      <assembly fullname="Newtonsoft.Json">
        <type fullname="*" />
      </assembly>
    </linker>

Linker in xamarin xamarin 中的链接器

You are probably better off by inheriting from ViewRenderer and providing the CardContainer as the native view type.通过从 ViewRenderer 继承并提供 CardContainer 作为本机视图类型,您可能会更好。 Then override the ViewRenderer's OnElementChanged method and construct a new CardContainer for the renderer.然后覆盖 ViewRenderer 的 OnElementChanged 方法并为渲染器构造一个新的 CardContainer。 Below is a short version that would get you started.下面是一个简短的版本,可以帮助您入门。

Registration:登记:

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

Class definition:类定义:

 SwipeableCardRenderer : ViewRenderer<SwipeableCard, CardContainer>

OnElementChanged override: OnElementChanged 覆盖:

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

Xamarin Forms PCL: System.MissingMethodException: Default constructor not found for type Xamarin Forms PCL:System.MissingMethodException:找不到类型的默认构造函数

This will occur normally whenever you used a class for dependency injection or using device-specific classes.每当您使用类进行依赖注入或使用特定于设备的类时,通常都会发生这种情况。 You can try it out by debugging.您可以通过调试来尝试一下。 I had faced the same but solved it by giving the current class for typeof property.我遇到了同样的问题,但通过为typeof属性提供当前类来解决它。 should not give the service or any other interface name:不应提供服务或任何其他接口名称:

[assembly: Xamarin.Forms.Dependency(typeof(Name Of Class))]

and not:并不是:

[assembly: Xamarin.Forms.Dependency(typeof(Name Of Interface))]

in Xamarin after update all nuget packages me also facing this issue.在 Xamarin 更新所有 nuget 包后,我也面临这个问题。

Finally i use some indirect fix最后我使用了一些间接修复

i called a class by dummy我用假人叫了一堂课

ex (isLogged == "55" is never reached) ex (isLogged == "55" 永远不会到达)

    //added this code to fix default constructor issue for appshell pages
    if(isLogged == "55")
    {
        MainPage = new HomePage();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM