Xamarin DependencyService: System.MissingMethodException: 找不
本文关键字:找不 MissingMethodException System Xamarin DependencyService | 更新日期: 2023-09-27 17:56:35
我在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();
}
苹果:
[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);
我的错误,再次:"系统.缺少方法异常:找不到类型Enchantum.Functions.PaymentProcessor的默认构造函数"
出了什么问题?
附言请原谅我对界面的错误命名约定以及其他一些混乱。
就我而言,问题出在程序集导出行上。
应用程序崩溃,因为我使用接口类型而不是类实现:
[assembly: Xamarin.Forms.Dependency(typeof(IServiceType))]
但正确的方法是使用特定于平台的接口实现:
[assembly: Xamarin.Forms.Dependency(typeof(ServiceImplementation_Android))]
也许你可以尝试让你的接口实现类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
}
}
我对链接器有同样的问题,当我将链接器设置为无时,它的工作
当我定义初始化只读私有属性的默认构造函数时,我遇到了同样的错误。
看起来很疯狂,但考虑到 Java 没有只读的概念,并且该属性可能转换为常量,这是有道理的。