NullReferenceException when accessing GUI Componen

2019-07-07 07:39发布

In Mono I have a simple NSWindow with Controller on it I drop a NSSplitView and a NSButton.

If I try to access the NSSplitView out of the Contstructor or Initialize() Method I get a nullReferenceException. Instead if I try to access the NSSplitView from a ButtonClicked Method, it works.

Well, it seems that the Framework creates the GUI Components after the controller's Construtor is called. But where should I put my code to configure GUI Components if not in the Constructor?

Thanks in advance. Johannes

2条回答
可以哭但决不认输i
2楼-- · 2019-07-07 08:09

You should use the AwakeFromNib method in your controller class. It is called once all the objects have been loaded and properly connected.

public override void AwakeFromNib ()
{
    base.AwakeFromNib ();

    // Do something here with the outlets
}
查看更多
爷的心禁止访问
3楼-- · 2019-07-07 08:15

You should use

public override void ViewDidLoad() {
    base.ViewDidLoad();
}

for configuring your view because that's the method which is called when the view just loaded, not when you instantiated the controller. If you configure your view in AwakeFromNib() you'll force the instantiation of your view whereas you maybe don't need them now. Indeed, when you create a controller the view are not created until someone try to access viewController.View.

查看更多
登录 后发表回答