构造函数中访问GUI组件时发生NullReferenceExce

构造函数中访问GUI组件时发生NullReferenceExce

本文介绍了在Controllers构造函数中访问GUI组件时发生NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Mono中,我有一个带有控制器的简单NSWindow,我放了一个NSSplitView和一个NSButton.

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

如果尝试从Contstructor或Initialize()方法之外访问NSSplitView,则会收到nullReferenceException.相反,如果我尝试从ButtonClicked方法访问NSSplitView,它将起作用.

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.

好吧,似乎框架在调用控制器的构造函数之后创建了GUI组件.但是,如果不在构造函数中,我应该将我的代码放在哪里以配置GUI组件?

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?

先谢谢了.约翰内斯

推荐答案

您应该在控制器类中使用AwakeFromNib方法.一旦所有对象均已加载并正确连接,便会调用它.

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
}

这篇关于在Controllers构造函数中访问GUI组件时发生NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 06:45