【问题标题】:Actionscript: how can I add a custom property to an instance of Image?Actionscript:如何将自定义属性添加到 Image 实例?
【发布时间】:2010-09-24 13:35:12
【问题描述】:
var image:Image = new Image();
image.property_1 = "abcdefg";

它不能像 Flash builder 所说的那样编译:

Description  Resource     Path      Location       Type
1119: Access of possibly undefined property jdkfjds through a reference with static type mx.controls:Image.
             adm.mxml     /adm/src  Line 209       Flex Problem

我该怎么做?或者我需要扩展 Image 类?挺无聊的。

【问题讨论】:

    标签: apache-flex actionscript-3 actionscript


    【解决方案1】:

    如果您知道需要添加的所有属性并且它们不会(经常)更改,那么这将起作用。

    package mypackage
    {
        import mx.controls.Image;
        public class ExtendedImage extends Image
        {
            public function ExtendedImage()
            {
                super();
            }
    
            //option 1
            private var prop1:String = "";
            public function get property_1():String
            {
                return prop1;
            }
            public function set property_1(val:String):void
            {
                prop1 = val;
            }
    
            //option 2
            public var property_2:String = "";
        }
    }
    

    【讨论】:

      【解决方案2】:

      Image 未声明为dynamic class,因此您无法动态添加属性。

      Image 派生并使用 dynamic 关键字扩展类:

      package mypackage
      {
        import mx.controls.Image;
      
        public dynamic class DynamicImage extends Image {}
      }
      

      现在这将起作用:

      import mypackage.DynamicImage;
      
      var image:DynamicImage = new DynamicImage();
      image.property_1 = "abcdefg";
      

      【讨论】:

        【解决方案3】:

        但是你可以为你的图片使用容器并在那里添加额外的属性;或者使用字典,图像作为键,属性作为值。

        【讨论】:

        • 容器可以接收 MouseEvent.CLICK 事件吗?
        • 实际上,子类化很简单,就像 splash 展示的那样,我更喜欢这种方法。
        猜你喜欢
        • 1970-01-01
        • 2011-01-24
        • 1970-01-01
        • 2012-07-18
        • 2012-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多