1

我正在尝试使用UINavigationController(Type navigationBarType, Type toolbarType)构造函数来指定UINavigationBar在 C# 中定义的自定义派生类。

但是,我的所有尝试都会导致 null ref 异常。我认为这与我的托管课程没有找到本机课程有关吗?

if (UIDevice.CurrentDevice.CheckSystemVersion(7, 0))
    rootNavController = new UINavigationController(typeof(UnderlayNavigationBar), null);
else
    rootNavController = new UINavigationController();

而不是第二个空参数,传入默认值typeof(UIToolbar)也无济于事。

System.NullReferenceException: Object reference not set to an instance of an object
  at MonoTouch.ObjCRuntime.Runtime.ConstructNSObject[NSObject] (IntPtr ptr, System.Type type, MissingCtorResolution missingCtorResolution) [0x00037] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:365
  at MonoTouch.ObjCRuntime.Runtime.ConstructNSObject (IntPtr ptr, IntPtr klass, MissingCtorResolution missingCtorResolution) [0x00013] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:348
  at MonoTouch.ObjCRuntime.Runtime.GetNSObject (IntPtr ptr, MissingCtorResolution missingCtorResolution) [0x00021] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:430
  at MonoTouch.ObjCRuntime.Runtime.TryGetOrConstructNSObjectWrapped (IntPtr ptr) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:658
  at at (wrapper native-to-managed) MonoTouch.ObjCRuntime.Runtime:TryGetOrConstructNSObjectWrapped (intptr)
  at at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging:void_objc_msgSend_IntPtr (intptr,intptr,intptr)
  at MonoTouch.UIKit.UINavigationController.set_ViewControllers (MonoTouch.UIKit.UIViewController[] value) [0x00028] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UINavigationController.g.cs:345

如果有人对我使用此构造函数的原因感兴趣,我将移植本指南:http ://b2cloud.com.au/how-to-guides/custom-uinavigationbar-colors-in-ios7

4

1 回答 1

6

我应该更详细地阅读调用堆栈,尤其是GetNSObject (IntPtr ptr, MissingCtorResolution missingCtorResolution).

现在完全有道理。该实例是用本机代码构造的,因此需要采用本机实例的 IntPtr 的托管构造函数重载。将该构造函数添加到我的派生类中解决了它。

public UnderlayNavigationBar(IntPtr handle) : base(handle)
{
}
于 2013-10-18T00:56:53.413 回答