繁体   English   中英

Xamarin DependencyService:System.MissingMethodException:找不到[Interface]的默认构造函数

[英]Xamarin DependencyService: System.MissingMethodException: Default constructor not found for [Interface]

我在Xamarin.Forms PCL上使用Dependency Service时收到此错误。 我已经看到了涉及iOSLinker此错误的答案。 但是,我在Android上运行它并且Linker已关闭。 调试模式也是如此。

它告诉我的构造函数无法在PCL中找到接口的默认构造函数。

我认为这可能是我做过的一些重命名问题(我使用了重构工具并确保所有必要的更改都已完成)所以我删除了那些文件夹/文件并重新制作它们。 依然没有 :(

我一直在搜索和调试这几个小时。 有什么东西可能导致这个错误? 我很确定我的DependencyService实现是正确的,所以我觉得它是不同的东西。

这是我的相关代码。

接口:

namespace Enchantum.Functions
{
public interface PaymentProcessor
{

    void setUpCard(String cardNumber,
        int expirationMonth,
        int expirationYear,
        String CVC);

    Task cardTokenizer();

    void backendCardCharge();

}

安卓:

[assembly: Xamarin.Forms.Dependency(typeof(PaymentProcessor_Android))]

namespace Enchantum.Droid.Functions_Android
{
class PaymentProcessor_Android : PaymentProcessor
{
public PaymentProcessor_Android() { }

    private Card mCard;
    private Token mToken;

    public void setUpCard(String cardNumber, int expirationMonth,
        int expirationYear, String cvcCode)
    {
        Card card = new Card
        {
            Number = cardNumber,
            ExpiryMonth = expirationMonth,
            ExpiryYear = expirationYear,
            CVC = cvcCode
        };

        mCard = card;
    }

    public async Task cardTokenizer()
    {
        mToken = await StripeClient.CreateToken(mCard);
    }

    /*TODO: 
     * */

    public void backendCardCharge()
    {
        throw new NotImplementedException();
    }

iOS版:

[assembly: Xamarin.Forms.Dependency(typeof(PaymentProcessor_iOS))]

namespace Enchantum.iOS.Functions_iOS
{
class PaymentProcessor_iOS : PaymentProcessor
{
public PaymentProcessor_iOS() { }

    private Card mCard;
    private Token mToken;

    public void setUpCard(String cardNumber, int expirationMonth,
        int expirationYear, String cvcCode)
    {
        Card card = new Card
        {
            Number = cardNumber,
            ExpiryMonth = expirationMonth,
            ExpiryYear = expirationYear,
            CVC = cvcCode
        };

        mCard = card;

    }

    public async Task cardTokenizer()
    {
        mToken = await StripeClient.CreateToken(mCard);
    }

    /*TODO:*/
    public void backendCardCharge()
    {
        throw new NotImplementedException();
    }




}

在Xamarin.Form内容页面中实现:

DependencyService.Get<PaymentProcessor>().setUpCard(
                    cardNumber,
                    expirationMonth,
                    expirationYear,
                    CVC);

我的错误,再次:“System.MissingMethodException:找不到类型Enchantum.Functions.PaymentProcessor的默认构造函数”

出了什么问题?

PS Pardon我对接口的错误命名约定以及其他一些混乱。

也许你可以尝试将你的接口实现类public ,你的构造函数是可见的,但类本身可能不是。

所以喜欢:

[assembly: Xamarin.Forms.Dependency(typeof(PaymentProcessor_Android))]

namespace Enchantum.Droid.Functions_Android
{
public class PaymentProcessor_Android : PaymentProcessor //make the class public
{

 //your code here

 }
}

我对链接器有同样的问题,当我将Linker设置为None时,它的工作原理

就我而言,问题出在程序集导出行中。
应用程序崩溃,因为我使用接口类型而不是类实现:
[assembly: Xamarin.Forms.Dependency(typeof(IServiceType))]

正确的方法是使用接口的Platform-Specific实现:
[assembly: Xamarin.Forms.Dependency(typeof(ServiceImplementation_Android))]

当我定义一个初始化readonly私有属性的默认构造函数时,我得到了同样的错误。

看起来很疯狂,但考虑到Java没有readonly的概念,它有意义,并且属性可能会转化为常数。

暂无
暂无

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

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