c# - MonoTouch 中自定义 TabBarController 中的 ViewDidLoad 中的公共(public)属性始终为 null

标签 c# controls xamarin.ios uitabbarcontroller viewdidload

我一直在研究从 UITabBarController 派生的类。在最基本的层面上,我想要做的是添加一个 BackgroundColor 属性和其他公共(public)属性,我可以在构建 TabBarController 时在我的 AppDelegate 中对其进行实例化。

我遇到的问题是,我可以在调用 ViewDidLoad 时让所有公共(public)属性最终为空,或者,我可以添加一个构造函数和一个 [Register] 属性,并最终使属性不为空,但是 ViewControllers 属性(包含所有选项卡)莫名其妙地变为 null(即使您在 ViewDidLoad 中设置它)。

显然,我需要两件事都是真实的,但我遗漏了一些特定的东西。

这是始终导致空 BackgroundColor 的代码版本,即使我在 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)属性,这显然会阻碍我编写一些自定义控件的能力。有谁知道我哪里出错了?

最佳答案

在调用 ViewDidLoad 之前必须发生一些事情。它们可以在构造函数中完成。但是下面的构造函数是坏的:

 public TabBarController() : base(NSObjectFlag.Empty)

因为它不允许 UITabController 默认 ctor 执行 - 它的工作是向“init”选择器发送消息。

我觉得你想要的有点像:

public class TabBarController : UITabBarController
{
    UIViewController one = new UIViewController ();
    UIViewController two = new UIViewController ();
    UIViewController three = new UIViewController ();

    private UIView myTabView;

    public UIColor BackgroundColor { 
        get { return myTabView.BackgroundColor; }
        set { myTabView.BackgroundColor = value; }
    }       

    public TabBarController()
    {
        var frame = new RectangleF(0.0f, 0.0f, this.View.Bounds.Size.Width, 46);
        myTabView = new UIView(frame);
        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 };
    }
}

    public override bool FinishedLaunching (UIApplication app, NSDictionary options)
    {
        TabBarController controller = new TabBarController ();
        // change background (to cyan) works before adding subview
        controller.BackgroundColor = UIColor.Cyan;
        window.AddSubview (controller.View);
        // change background (to blue) works after adding subview
        controller.BackgroundColor = UIColor.Blue;
... 

编辑:删除了 .ctor 中的无操作背景设置。添加了 FinishedLaunching 示例代码。

关于c# - MonoTouch 中自定义 TabBarController 中的 ViewDidLoad 中的公共(public)属性始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7142441/

相关文章:

C# 二维整数数组赋值和排序

c# - 长时间运行的任务阻塞了 UI

c# - 如何从位创建字符串?

xamarin.ios - MonoDevelop Intellisense 不显示 PCL 中的所有项目

ios - Xamarin iOS 如何使用相同的 ViewController 为 iPad 和 iPhone 制作不同的 xib

c# - System.Reflection - 如何判断 MethodInfo 对象是方法访问​​器还是属性访问器?

Python主控

windows - 当后续列有图像时,列表控件将图像的空间添加到列 0

javascript - 有没有更有效的方法来对对象中的数组中的数组进行排序?

c# - Sql azure - 单触摸