Xamarin使用自定义UINavigationBar将UINavigationController子类化(Xamarin subclassed UINavigationController with

编程入门 行业动态 更新时间:2024-10-22 19:48:33
Xamarin使用自定义UINavigationBar将UINavigationController子类化(Xamarin subclassed UINavigationController with custom UINavigationBar)

我正在尝试创建一个非标准的顶部导航栏,以便在整个应用程序中使用。 为了实现这一点,我一直在尝试子类化UINavigationController和UINavigationBar

我有一个自定义的NavigationController类

partial class ZooNavigationController : UINavigationController { public ZooNavigationController (IntPtr handle) : base (typeof(TopNavBar), null) { this.Handle = handle; } }

它指向基础构造函数

public UINavigationController (Type navigationBarType, Type toolbarType);

对于我的自定义UINavigationBar类TopNavBar ,它类似于......

public class TopNavBar : UINavigationBar { public TopNavBar () { InitCustom (); } public void InitCustom(){ this.BackgroundColor = UIColor.Red; // a bunch more custom stuff } }

问题是,当我运行它时,从不调用TopNavBar。 如果我尝试将我的构造函数调整为如下所示:

public ZooNavigationController (IntPtr handle) : base (typeof(TopNavBar), null) { this.Handle = handle; TopNavBar test = (TopNavBar)this.NavigationBar; }

我得到一个运行时异常,它无法转换类型,因此它似乎忽略了我指定UINavigationBar类型的调用。

任何人都可以帮助我解决我在这里失踪的问题吗?

编辑最后我发现我错过了你可以在故事板中设置自定义UINavigationBar的事实。 结合米格尔的回答,我最终得到了这门课程

partial class TopNavBar : UINavigationBar { public TopNavBar(IntPtr test) : base(test) { } [Export ("initWithCoder:")] public TopNavBar (NSCoder coder) : base (coder) { InitCustom (); } }

I'm trying to create a non-standard top navigation bar for use throughout my application. To achieve this I've been trying to subclass UINavigationController and UINavigationBar

I have a custom NavigationController class

partial class ZooNavigationController : UINavigationController { public ZooNavigationController (IntPtr handle) : base (typeof(TopNavBar), null) { this.Handle = handle; } }

which points to the base constructor

public UINavigationController (Type navigationBarType, Type toolbarType);

for my custom UINavigationBar class TopNavBar which is something like...

public class TopNavBar : UINavigationBar { public TopNavBar () { InitCustom (); } public void InitCustom(){ this.BackgroundColor = UIColor.Red; // a bunch more custom stuff } }

The problem is, TopNavBar is never called when I run this. If I try to adjust my constructor to look like this:

public ZooNavigationController (IntPtr handle) : base (typeof(TopNavBar), null) { this.Handle = handle; TopNavBar test = (TopNavBar)this.NavigationBar; }

I get a runtime exception that it can't cast the types, so it seems that it's ignoring my call specifying the UINavigationBar type.

Can anyone help me out with what I'm missing here?

EDIT in the end it turns out I was missing the fact that you can set a custom UINavigationBar inside the storyboard. Combining that with the miguel's answer I ended up with the class

partial class TopNavBar : UINavigationBar { public TopNavBar(IntPtr test) : base(test) { } [Export ("initWithCoder:")] public TopNavBar (NSCoder coder) : base (coder) { InitCustom (); } }

最满意答案

调用IntPtr构造函数以响应Objective-C创建的对象并浮出C#。 这通常不是实例化这些类的方式。

你要问自己的第一个问题是:谁在创建你的班级实例?

您可以从C#创建此类的实例:使用适当的参数调用构造函数。 在反序列化期间创建(例如,从故事板,XIB或您自己的归档数据加载),然后您需要提供带有NSCoder参数的构造函数。 根据需要重新创建实例(可能表示存在问题,因为它意味着您的对象已被销毁,但Objective-C保留了对它的引用,现在又重新进行了表面处理),即IntPtr构造函数。

在上面的例子中,有一个错误:你重写了IntPtr构造函数,它只能调用基类IntPtr构造函数(因为它意味着“我有一个指向objective-c中真实对象的指针,为它创建一个包装器它”)。

我的猜测是你正在使用C#,所以在这种情况下,你想要的是提供一个不带参数的新构造函数:

public ZooNavigationController () : base (typeof (YourNavigation), typeof(YourBar)) { // Your own initialization goes here }

The IntPtr constructor is called in response to the object being created by Objective-C and surfaced to C#. This in general is not the way that these classes are instantiated.

The first question that you have to ask yourself is: who is creating the instance of your class?

You create the instance of this class from C#: you call a constructor with the proper parameters. Being created during deserialization (for example, loading from a storyboard, XIB, or your own archived data), then you need to provide the constructor that takes an NSCoder parameter. Having your instance recreated on demand (may indicate a problem, because it means that your object was destroyed, but Objective-C kept a reference to it, and now it is being resurfaced again), the IntPtr constructor.

In your example above, there is a mistake: you are overriding the IntPtr constructor, which should only ever call into the base class IntPtr constructor (since it means "I have a pointer to the real object in objective-c, create a wrapper for it").

My guess is that you are using C#, so in that case, what you want is to provide a new constructor that takes no arguments:

public ZooNavigationController () : base (typeof (YourNavigation), typeof(YourBar)) { // Your own initialization goes here }

更多推荐

本文发布于:2023-07-31 02:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340536.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子类   自定义   UINavigationBar   Xamarin   custom

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!