Compact Framework的UserControl上没有加载事件?

时间:2009-11-03 14:57:37

标签: compact-framework load user-controls

看起来CF上的usercontrol没有Load事件。

我习惯于在Load EventHandler上加载数据。

为CF实现这一目标的另一种方法是什么?

到目前为止看起来我别无选择,只能在用户控件的Contructor中这样做...

3 个答案:

答案 0 :(得分:3)

我删除了之前的答案,因为它绝对是垃圾。你100%正确,CF没有Load事件。事实证明,当我编写UserControl时,我自己发现了这一点。我在一个旧项目中查找了load事件,其中我的所有用户控件都是从我自己的基类继承的......名为UserControlBase。

我似乎在我的基类中实现了自己的Load功能。这是我的基类的缩减版本:

public class UserControlBase : UserControl
{

    public UserControlBase() { }

    public event EventHandler Load;

    private void Loaded()
    {
        if (this.Load != null)
        {
            this.Load(this, new EventArgs());
        }
    }

    public new void ResumeLayout()
    {
        this.Loaded();
        base.ResumeLayout();
    }

    public new void ResumeLayout(bool performLayout)
    {
        this.Loaded();
        base.ResumeLayout(performLayout);
    }

}

现在,在您的实际用户控件中,执行以下操作:

public partial class UserControl1 : UserControlBase
{

    public UserControl1()
    {
        InitializeComponent();
    }

    private void UserControl1_Load(object sender, EventArgs e)
    {

    }

}

这是一个非常古老的项目。在以后的项目中,我倾向于在.Load()中调用用户控件的Form_Load方法。

答案 1 :(得分:0)

您可以使用EnabledChanged事件并检查控件是否首次启用:

private void UserControl1_EnabledChanged(object sender, EventArgs e)
{
    if (Enabled && firstTime)
    {
        firstTime= false;
        //DO init
    }
}

答案 2 :(得分:-1)

我会使用OnHandleCreated,但请记住,可以重新创建Control的句柄。

E.x:



private bool _loaded;

protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
if(!_loaded)
{
_loaded=true;
DoLoad();
}
}

private void DoLoad()
{
}