Xamarin.ios 找不到ViewController::.ctor(System.IntPtr)的构造函数

Xamarin.ios 找不到ViewController::.ctor(System.IntPtr)的构造函数,xamarin.ios,Xamarin.ios,我遇到了一个问题,我的Monotouch应用程序有时在收到内存警告后崩溃。请查看下面的堆栈跟踪 Received memory warning. Level=2 DTMobileIS[2299] : _memoryNotification : { OSMemoryNotificationLevel = 2; timestamp = "2011-04-11 14:29:09 +0000"; } Toplevel exception: System.Missi

我遇到了一个问题,我的Monotouch应用程序有时在收到内存警告后崩溃。请查看下面的堆栈跟踪

Received memory warning. Level=2 DTMobileIS[2299] : _memoryNotification : { OSMemoryNotificationLevel = 2; timestamp = "2011-04-11 14:29:09 +0000"; } Toplevel exception: System.MissingMethodException: No constructor found for Myapp.UI.BoardController::.ctor(System.IntPtr) at System.Activator.CreateInstance (System.Type type, BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes) [0x00000] in :0 at System.Activator.CreateInstance (System.Type type, System.Object[] args, System.Object[] activationAttributes) [0x00000] in :0 at System.Activator.CreateInstance (System.Type type, System.Object[] args) [0x00000] in :0 at MonoTouch.ObjCRuntime.Runtime.ConstructNSObject (IntPtr ptr, IntPtr klass) [0x00000] in :0 at MonoTouch.ObjCRuntime.Runtime.GetNSObject (IntPtr ptr) [0x00000] in :0 at MonoTouch.ObjCRuntime.Runtime.GetNSObjectWrapped (IntPtr ptr) [0x00000] in :0 at (wrapper native-to-managed) MonoTouch.ObjCRuntime.Runtime:GetNSObjectWrapped (intptr) at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00000] in :0 at Myapp.Free.Application.Main (System.String[] args) [0x00000] in /Users/haakon/Code/Myapp-work/iOS/Myapp.Free/Myapp.Free/Main.cs:12 收到内存警告。级别=2 DTMobileIS[2299]:_记忆通知:{ OSMemoryNotificationLevel=2; timestamp=“2011-04-11 14:29:09+0000”; } 顶级异常:System.MissingMethodException:未找到Myapp.UI.BoardController::.ctor(System.IntPtr)的构造函数 在System.Activator.CreateInstance(System.Type类型、BindingFlags bindingAttr、System.Reflection.Binder Binder、System.Object[]参数、System.Globalization.CultureInfo区域性、System.Object[]activationAttributes)[0x00000]中:0 在System.Activator.CreateInstance(System.Type类型,System.Object[]args,System.Object[]activationAttributes)[0x00000]中:0 在:0中的System.Activator.CreateInstance(System.Type类型,System.Object[]args)[0x00000]处 在MonoTouch.ObjCRuntime.Runtime.ConstructNSObject(IntPtr ptr,IntPtr klass)[0x00000]中:0 在:0中的MonoTouch.ObjCRuntime.Runtime.GetNSObject(IntPtr ptr)[0x00000]处 在:0中的MonoTouch.ObjCRuntime.Runtime.GetNSObjectWrapped(IntPtr ptr)[0x00000]处 at(包装器本机到托管)MonoTouch.ObjCRuntime.Runtime:GetNSObjectWrapped(intptr) 在:0中的MonoTouch.UIKit.UIApplication.Main(System.String[]args,System.String principalClassName,System.String delegateClassName)[0x00000]处 在Myapp.Free.Application.Main(System.String[]args)[0x00000]in/Users/haakon/Code/Myapp-work/iOS/Myapp.Free/Myapp.Free/Main.cs:12 堆栈跟踪是正确的,因为指定的类(BoardController,它是UIViewController子类)缺少采用IntPtr参数的构造函数。但这是故意的,因为我根本不在应用程序中使用Interface Builder。那么为什么会发生这种情况呢

我确实发现了一个类似的问题,它似乎表明,如果您允许您的视图(或可能的视图控制器)被垃圾收集,则可能会发生这种情况。但我不明白这怎么会发生在这里。一些背景:我的应用程序委托持有对导航控制器的强引用,而导航控制器又持有对导航堆栈中根视图控制器的强引用。此根视图控制器还保存对BoardController实例的强引用。所以我不明白BoardController怎么可能被垃圾收集


有什么想法吗?

当本机对象需要呈现给托管对象时,会使用IntPtr构造函数。在这种情况下,您可以创建如下内容:

 var foo = new Foo ();
 SomeObject.Property = foo;
这会将Foo对象分配给属性,但是如果属性是Objective-C对象,如果您没有保留对“Foo”的引用,那么Mono的GC将继续处理托管Foo和非托管Foo之间的链接

然后,稍后,您将尝试检索它:

 var bar = SomeObject.Property;
在这里,MonoTouch将知道,除了映射到它之外,再也没有托管对象了,因此它必须构建一个新的对象,但它所拥有的只是Objective-C代码的IntPtr。这就是为什么您需要构造函数

您可能只想添加本机构造函数,这在许多情况下都很好,但如果您的对象在托管世界中存储了自己的状态,则会出现问题,例如:

 public class Foo : UIView {
     string Name;
     public Foo () { Name= "Hello"; }
     public Foo (IntPtr ptr) : base (ptr) {}
 }
在这种情况下,IntPtr构造函数不能仅从IntPtr完全重构托管对象及其状态。因此,问题的可能根源在于没有保留对对象的引用


因此,简而言之,真正的解决办法是:在托管代码中保留对BoardController的引用,以防止在以后仍要使用该对象时收集该对象。

谢谢。你。米格尔!!!!我已经为此奋斗了很久。我得到了一些时髦的nav/tab/single/dialog根视图控制器更改逻辑+模态控制器,只是花了太长时间玩那些东西,认为这就是原因。我不知道是什么让我花了这么长的时间来搜索例外消息。。。但现在一切都变得完全有意义了!