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


我在使用依赖服务时收到此错误 Xamarin.Forms PCL。我已经看到了涉及此错误的答案 iOS 和 Linker。 但是,我正在运行它 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的默认构造函数”

出了什么问题?

附: 请原谅我对接口的错误命名约定以及其他一些混乱。


2795
2017-07-13 03:39


起源



答案:


也许你可以尝试制作你的接口实现类 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

 }
}

8
2017-07-13 07:16



对于遇到此问题的任何其他人,并想知道为什么您的代码似乎不太正确。依赖关系定义应包括当前类, PaymentProcessor_Android 不是界面, PaymentProcessor。 - Brad Moore
如上例所示,我的错误在于写作 [assembly: Xamarin.Forms.Dependency(typeof(PaymentProcessor))] 我收到同样的错误。 - Enrico
在我的情况下,构造函数受到保护,将其更改为public就行了! - chaosifier
我抓了一会儿,但是必须要这样做 [assembly: Dependency(typeof([Your_Type))] 线。我不知道所有的解析器是否都需要这条线,所以这可能会让我失望,但是Xamarin Forms会这样做。 - Chucky


答案:


也许你可以尝试制作你的接口实现类 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

 }
}

8
2017-07-13 07:16



对于遇到此问题的任何其他人,并想知道为什么您的代码似乎不太正确。依赖关系定义应包括当前类, PaymentProcessor_Android 不是界面, PaymentProcessor。 - Brad Moore
如上例所示,我的错误在于写作 [assembly: Xamarin.Forms.Dependency(typeof(PaymentProcessor))] 我收到同样的错误。 - Enrico
在我的情况下,构造函数受到保护,将其更改为public就行了! - chaosifier
我抓了一会儿,但是必须要这样做 [assembly: Dependency(typeof([Your_Type))] 线。我不知道所有的解析器是否都需要这条线,所以这可能会让我失望,但是Xamarin Forms会这样做。 - Chucky


我对Linker也有同样的问题,当我将Linker设置为None时,它的工作正常


2
2017-11-29 10:19





当我定义一个初始化a的默认构造函数时,我得到了同样的错误 只读 私人财产。

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


0
2017-07-10 21:41