如何设置属性的初始化顺序? [英] How to set a property's initialization order?

查看:89
本文介绍了如何设置属性的初始化顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.这是代码:

Hi. This is the code:

public partial class myControl : UserControl
{
    private int imageIndex = -1;
    private ImageList images = new ImageList();

    public myControl()
    {
        InitializeComponent();
    }

    public ImageList Images
    {
        get{ return images; }
        set{
            if ((value != null) && (value.Images.Count > imageIndex))
                images = value;
        }
    }

    public int ImageIndex
    {
        get{ return imageIndex; }
        set{
            if ((Images.Images.Count > 0) && (Images.Images.Count > value))
                imageIndex = value;
        }
    }
}



将myControl放在设计器中的窗体"上时,可以在Properties Editor中编辑属性的值.这样,VS会自动创建Designer generated code,该Designer generated code会使用我在Properties Editor中输入的值来初始化我的属性.

从上面的代码中可以看到,除非Images属性已初始化并且包含我要为ImageIndex设置的索引值,否则我不允许ImageIndex设置值.
当设计师生成代码时,它看起来像这样:



When I put myControl on a Form in the designer, I can edit the values of the properties in the Properties Editor. By doing so, VS automatically creates the Designer generated code that initializes my properties with the values I put in the Properties Editor.

As you can see from the code above, I don''t allow the ImageIndex to set a value, unless the Images property is initialized and contains the index value I want to set for ImageIndex.

When the designer generates the code it looks like this:

this.myControl1.ImageIndex = 1; //this is first and has no effect
//the ImageIndex is still -1 because Images is not yet set

this.myControl1.Images = this.mainImageList; //this is last



首先是ImageIndex 属性,然后是Images .这样,ImageIndex会尝试设置值1,但不能这样做,因为Images为空.

代码应如下所示:



First is the ImageIndex property and then Images . By doing so the ImageIndex tries to set value 1 but cannot because Images is empty.

The code should look like this:

this.myControl1.Images = this.mainImageList; //firs I initialize the image list

this.myControl1.ImageIndex = 1; //second I initialize the image index
//the ImageIndex is now 1 
//it is allowed because the image list contains 2 images



我想让设计人员更改自动生成的初始化代码中属性的顺序.这样,它将首先初始化Images,然后在此ImageIndex之后.

有人可以帮忙吗?



I want to make the designer change the order of the properties in the initialization auto-generated code. This way it will first initialize Images and after this ImageIndex.

Can anyone help with this?

推荐答案

在我看来,您需要实现ISupportInitialize接口.

请参阅 [
It sounds to me that you need to implement the ISupportInitialize Interface.

See this[^] from MSDN.

If you get stuck on specifics, please ask again.

Good luck! :)


然后只需切换它们即可.您可以编辑设计者生成的代码.您可能只需要检查以确保它不会还原为VS想要它执行的操作即可.

您也可以只允许它进行设置...从set中删除if语句,如果count = 0或imageindex>则将get更改为返回-1.计数.
just switch them then. You can edit the designer-generated code. You just may have to check to make sure that it doesn''t revert back to what VS wants it to do.

You could also just allow it to set it...remove your if statement from the set and change your get to return -1 if count = 0 or imageindex > count.


这篇关于如何设置属性的初始化顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆