使用UINavigationController(Type navigationBarType,Type toolbarType)构造函数指定自定义导航栏

Tys*_*son 1 c# xamarin.ios ios

我试图使用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();
Run Code Online (Sandbox Code Playgroud)

传递默认值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
Run Code Online (Sandbox Code Playgroud)

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

Tys*_*son 6

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

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

public UnderlayNavigationBar(IntPtr handle) : base(handle)
{
}
Run Code Online (Sandbox Code Playgroud)