23

Xamarin.Forms PCL. 我已经看到这个错误的答案涉及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请原谅我不正确的接口命名约定以及其他一些混乱。

4

4 回答 4

14

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

 }
}
于 2016-07-13T07:16:15.097 回答
14

就我而言,问题出在装配出口线上。
当我使用接口类型而不是类实现时,应用程序崩溃了:
[assembly: Xamarin.Forms.Dependency(typeof(IServiceType))]

正确的方法是使用接口的平台特定实现:
[assembly: Xamarin.Forms.Dependency(typeof(ServiceImplementation_Android))]

于 2019-03-08T15:47:37.390 回答
5

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

于 2017-11-29T10:19:00.570 回答
0

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

看起来很疯狂,但考虑到 Java 没有 readonly 的概念,并且该属性可能已转换为常量,这是有道理的。

于 2017-07-10T21:41:12.567 回答