【发布时间】:2011-08-22 01:49:40
【问题描述】:
我一直在研究从 UITabBarController 派生的类。在最基本的层面上,我要做的是添加一个 BackgroundColor 属性和其他公共属性,我可以在构建 TabBarController 时在我的 AppDelegate 中实例化它们。
我遇到的问题是,当调用 ViewDidLoad 时,我可以让所有公共属性最终都为空,或者,我可以添加一个构造函数和一个 [Register] 属性,最终让属性不为空,但是 ViewControllers 属性(包含所有选项卡)变得莫名其妙地为空(即使您在 ViewDidLoad 中设置了它)。
显然,我需要这两件事都是真实的,但我缺少一些具体的东西。
即使我在 AppDelegate 中显式设置,以下代码版本始终会导致背景颜色为空:
public class TabBarController : UITabBarController
{
public UIColor BackgroundColor { get; set; }
public override ViewDidLoad()
{
base.ViewDidLoad();
if(BackgroundColor != null) // Always null, even when set explicitly
{
var frame = new RectangleF(0.0f, 0.0f, this.View.Bounds.Size.Width, 46);
UIView myTabView = new UIView(frame);
myTabView.BackgroundColor = BackgroundColor;
myTabView.Alpha = 0.5f;
this.TabBar.InsertSubview(myTabView, 0);
}
// Add tabs here, which show up correctly (on default background color)
ViewControllers = new UIViewController[] { one, two, three, etc };
}
}
这里是显示正确背景颜色的编辑代码(属性不为空),但不允许 ViewControllers 属性为空,即使它只是在 ViewDidLoad 中设置:
[Register("TabBarController")]
public class TabBarController : UITabBarController
{
public UIColor BackgroundColor { get; set; }
// Added a binding constructor
[Export("init")]
public TabBarController() : base(NSObjectFlag.Empty)
{
}
public override ViewDidLoad()
{
base.ViewDidLoad();
if(BackgroundColor != null) // Hey, this works now!
{
var frame = new RectangleF(0.0f, 0.0f, this.View.Bounds.Size.Width, 46);
UIView myTabView = new UIView(frame);
myTabView.BackgroundColor = BackgroundColor;
myTabView.Alpha = 0.5f;
this.TabBar.InsertSubview(myTabView, 0);
}
// Tabs disappear, ViewControllers is always null
ViewControllers = new UIViewController[] { one, two, three, etc };
if(ViewControllers == null)
{
Console.WriteLine("Not bro");
}
}
}
如果我必须显式添加所有元素而无法在运行时访问公共属性,这显然会阻碍我编写一些自定义控件的能力。有谁知道我哪里出错了?
【问题讨论】:
-
“public override ViewDidLoad()”将无法编译。请提供足够完整的内容以对希望帮助您的人有用:-)
标签: c# controls xamarin.ios uitabbarcontroller viewdidload