2013-12-18 31 views
2

在我Xamarin的應用程序時,我把這種方法Xamarin SKPaymentQueue AddPayment引發異常

private void MakePayment (SKProduct product) 
{ 
    SKPayment payment = SKPayment.PaymentWithProduct (product); 
    SKPaymentQueue.DefaultQueue.AddPayment (payment); 
} 

我得到這個錯誤:

Failed to marshal the Objective-C object 0x14607110 (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).

我不知道如果我有什麼錯誤配置或有在我的代碼或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 
+1

的'SKPaymentTransaction []非常相似'在'SKPaymentTransactionObserver'的'UpdatedTransactions'方法中使用,你可以發佈你的代碼給觀察者嗎? – jonathanpeppers

+0

我添加了觀察者代碼和完整的堆棧跟蹤。儘管如此,它似乎並沒有得到觀察員的注意。 – Aaron

+0

看起來這與項目上的鏈接器行爲有關。我改變它做不要鏈接,現在我沒有得到這個錯誤。我應該擁有它嗎?不要鏈接? – Aaron

回答

0

我真的不知道是什麼造成了這一點。我認爲這可能與鏈接器有關。但是我從Mac中完全卸載了Xamarin(包括所有的MonoTouch軟件)並重新安裝了所有軟件,現在它正在運行。

2

這裏發生的事情是你的CustomPaymentObserver的C#實例被垃圾收集,而它的本地(「Objective-C」)對象居住在其上。當通知最終傳遞時,本地對象試圖調用已死的C#對象並崩潰你的應用程序。

爲了規避這一點,請保留對您的CustomPaymentObserver的參考,例如在你的AppDelegate中保持活着。

我不知道這是什麼地方記錄了Xamarin.iOS(找不到上咋一看的東西),但我相信,Xamarin.Droid(如http://developer.xamarin.com/guides/android/advanced_topics/garbage_collection/