当我调用此方法时,在我的Xamarin应用程序中
private void MakePayment (SKProduct product)
{
SKPayment payment = SKPayment.PaymentWithProduct (product);
SKPaymentQueue.DefaultQueue.AddPayment (payment);
}
我收到此错误:
无法编组Objective-C对象0x14607110(类型: SKPaymentTransaction)。找不到现有的托管实例 这个对象,也不可能创建一个新的托管实例 (因为类型'MonoTouch.StoreKit.SKPaymentTransaction []'没有 有一个构造函数,它接受一个IntPtr参数。)
我不确定我的配置是否有错或我的代码或Xamarin中存在问题。
这是Observer的代码
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
答案 0 :(得分:2)
这里发生的事情是你的CustomPaymentObserver
的C#实例被垃圾收集,而它的本地(“Objective-C”)对应物仍然存在。当最终传递通知时,本机对象试图调用现在死亡的C#对象并使应用程序崩溃。
要避免这种情况,请保留对CustomPaymentObserver
的引用,例如在你的AppDelegate中保持活着。
我不确定Xamarin.iOS的某处是否有记录(快速找不到的东西),但我认为Xamarin.Droid非常相似(例如http://developer.xamarin.com/guides/android/advanced_topics/garbage_collection/)
答案 1 :(得分:0)
我不确定是什么导致了这一点。我认为这可能与链接器有关。但是我完全从我的Mac上卸载了Xamarin(包括所有的MonoTouch东西)并重新安装了所有东西,现在它正在工作。