gpt4 book ai didi

c# - 来自不同类的 UITextField null

转载 作者:太空狗 更新时间:2023-10-30 00:30:57 25 4
gpt4 key购买 nike

我有课

public ViewController1 (IntPtr handle) : base (handle){}

ViewController1.designer.cs 中我注册了一个 UITextField

[Outlet]
UIKit.UITextField _textField { get; set; }

这是我挣扎的地方:

我有另一个类 ViewController2 继承自 ViewController1

public class ViewController2 : ViewController1
{
public ViewController2 (IntPtr handle) : base (handle){}
}

为什么在 ViewController2 中,我的 _textField 在调试时总是 null

最佳答案

你初始化了吗_textField ?喜欢_textField = new UIKit.UITextField(); .因为创建类时所有字段都为空。创建后 ViewController1ViewController2 , _textField为 null,除非您在构造函数中为它赋值。

更准确地说,每个字段T field初始化为 default(T) ,对于引用类型,它是 null .

顺便说一句:您的“字段”是一个属性。但自动属性也是如此。

你可以初始化_textField在构造函数中像这样:

public partial class ViewController1
{
public ViewController1 (IntPtr handle)
: base(handle)
{
_textField = new UIKit.UITextField();
}
}

我必须承认我不了解 xamarin.你必须调用InitializeComponent();吗?在构造函数中?是否ViewController1.designer.cs有这样的方法吗?

    public ViewController1 (IntPtr handle)
: base(handle)
{
InitializeComponent();
}

例如,在 WinForms 中,设计器分部类有这样一个创建控件的方法。

关于c# - 来自不同类的 UITextField null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31755819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com