![](/img/trans.png)
[英]Xamarin Forms -> System.MissingMethodException: Default constructor not found for [Interface]
[英]Xamarin DependencyService: System.MissingMethodException: Default constructor not found for [Interface]
我在Xamarin.Forms PCL
上使用Dependency Service时收到此错误。 我已经看到了涉及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();
}
[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();
}
}
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.