Xamarin.ios Xamarin SKPaymentQueue AddPayment引发异常
在我的Xamarin应用程序中调用此方法时Xamarin.ios Xamarin SKPaymentQueue AddPayment引发异常,xamarin.ios,in-app-purchase,xamarin,Xamarin.ios,In App Purchase,Xamarin,在我的Xamarin应用程序中调用此方法时 private void MakePayment (SKProduct product) { SKPayment payment = SKPayment.PaymentWithProduct (product); SKPaymentQueue.DefaultQueue.AddPayment (payment); } 我得到这个错误: 无法封送Objective-C对象0x14607110(类型: SKPaymentTransactio
private void MakePayment (SKProduct product)
{
SKPayment payment = SKPayment.PaymentWithProduct (product);
SKPaymentQueue.DefaultQueue.AddPayment (payment);
}
我得到这个错误:
无法封送Objective-C对象0x14607110(类型:
SKPaymentTransaction)。找不到的现有托管实例
此对象,也不可能创建新的托管实例
(因为类型“MonoTouch.StoreKit.SKPaymentTransaction[]”没有
有一个接受一个IntPtr参数的构造函数)
我不确定我是否配置了错误的东西,或者我的代码或Xamarin中存在问题
这是观察员的代码
internal class CustomPaymentObserver : SKPaymentTransactionObserver
{
private InAppPurchase inAppPurchase;
public CustomPaymentObserver (InAppPurchase inAppPurchase)
{
this.inAppPurchase = inAppPurchase;
}
public override void UpdatedTransactions (SKPaymentQueue queue, SKPaymentTransaction[] transactions)
{
Console.WriteLine ("UpdatedTransactions");
foreach (SKPaymentTransaction transaction in transactions) {
switch (transaction.TransactionState) {
case SKPaymentTransactionState.Purchased:
inAppPurchase.CompleteTransaction (transaction);
break;
case SKPaymentTransactionState.Failed:
inAppPurchase.FailedTransaction (transaction);
break;
default:
break;
}
}
}
public override void PaymentQueueRestoreCompletedTransactionsFinished (SKPaymentQueue queue)
{
}
public override void RestoreCompletedTransactionsFailedWithError (SKPaymentQueue queue, NSError error)
{
}
}
以下是完整的堆栈跟踪:
System.Exception: Failed to marshal the Objective-C object 0x17ecb680 (type: SKPaymentTransaction). Could not find an existing managed instance for this object, nor was it possible to create a new managed instance (because the type 'MonoTouch.StoreKit.SKPaymentTransaction[]' does not have a constructor that takes one IntPtr argument).
at MonoTouch.ObjCRuntime.Runtime.MissingCtor (IntPtr ptr, IntPtr klass, System.Type type, MissingCtorResolution resolution) [0x00046] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/.pp-Runtime.cs:352
at MonoTouch.ObjCRuntime.Runtime.ConstructNSObject[NSObject] (IntPtr ptr, System.Type type, MissingCtorResolution missingCtorResolution) [0x00000] in :0
at MonoTouch.ObjCRuntime.Runtime.GetNSObject (IntPtr ptr, System.Type target_type, MissingCtorResolution missingCtorResolution, System.Boolean& created) [0x00073] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/.pp-Runtime.cs:514
at MonoTouch.ObjCRuntime.Runtime.GetNSObjectWrapped (IntPtr ptr, IntPtr type_ptr, System.Boolean& created) [0x0000c] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/.pp-Runtime.cs:686
at at (wrapper native-to-managed) MonoTouch.ObjCRuntime.Runtime:GetNSObjectWrapped (intptr,intptr,int&)
at at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging:void_objc_msgSend_IntPtr (intptr,intptr,intptr)
at MonoTouch.StoreKit.SKPaymentQueue.AddPayment (MonoTouch.StoreKit.SKPayment payment) [0x00014] in /Developer/MonoTouch/Source/monotouch/src/StoreKit/.pp-SKPaymentQueue.g.cs:109
at IOS.Util.IAP.InAppPurchase.ReceivedResponse (MonoTouch.StoreKit.SKProductsRequest request, MonoTouch.StoreKit.SKProductsResponse response) [0x0001d] in /Users/aaron/Projects/budget-ease-xamarin/IOS/Util/IAP/InAppPurchase.cs:43
at at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/.pp-UIApplication.cs:38
at IOS.Application.Main (System.String[] args) [0x00008] in /Users/aaron/Projects/budget-ease-xamarin/IOS/Main.cs:16
我真的不确定是什么原因造成的。我想这可能与链接器有关。但是我完全从我的Mac上卸载了Xamarin(包括所有MonoTouch的东西),并重新安装了所有东西,现在一切都正常了。这里发生的事情是,你的
CustomPaymentObserver
的C#实例被垃圾收集,而它的原生(“Objective-C”)副本却继续存在。当最终发送通知时,本机对象试图调用现已死亡的C#对象,并使您的应用程序崩溃
要避免这种情况,请在AppDelegate中保留对您的CustomPaymentObserver
的引用,以使其保持活动状态
我不确定Xamarin.iOS是否记录了这一点(快速查看时找不到),但我相信Xamarin.Droid(例如)在
SKPaymentTransactionObserver
的UpdatedTransactions
方法中使用了SKPaymentTransaction[]
,你能为观测者发布你的代码吗?我添加了观测者代码和完整的堆栈跟踪。不过,它似乎并没有到达观测者那里。这似乎与项目上的链接器行为有关。我改变了它不链接,现在我没有得到这个错误。我应该启用“不链接”吗?我只会使用链接SDK程序集
,你链接了所有内容吗?我只使用了链接SDK程序集,而且链接器似乎正在删除错误消息中提到的构造函数。我不知道为什么,但卸载Xamarin和MonoTouch并重新安装似乎已经解决了这个问题。