从扩展组件访问组件属性

时间:2011-07-29 16:15:13

标签: flex actionscript-3 flex4

我有一个声明了公共变量的组件

[Bindable]
public var mnuSource:String;

当我扩展这个组件时,我可以引用mnuSource(它编译)但是Runtime抱怨该属性不可访问(错误1056)。

如何修改/声明组件属性,以便它们实际可供其他组件使用?

由于

2 个答案:

答案 0 :(得分:0)

这是未经测试的代码,但应该可以正常工作并为您提供如何扩展类的好方法。

MyBaseClass.as
package{
  public class MyBaseClass{
    public var someVar:String;
    public function MyBaseClass( )::void{
      this.someVar = 'set from MyBaseClass';
    }
  }
}


MyBaseclassExtended.as
package{
  public class MyBaseclassExtended extends MyBaseClass{
    public MyBaseclassExtended( ){
      this.someVar = 'Set from MyBaseclassExtended';
    }
  }
}


call it like so
var asdf:MyBaseclassExtended = new MyBaseclassExtended();
trace( asdf.someVar ) // Set from MyBaseclassExtended

答案 1 :(得分:0)

与The_asMan相同,但在MXML中

应用

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" xmlns:local="*" 
               >
    <local:SomeExtendedComponent />
</s:Application>

一些基本组件

<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx">
    <fx:Script>
        <![CDATA[
            [Bindable]
            public var mnuSource:String;
        ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
</s:Group>

扩展组件

<?xml version="1.0" encoding="utf-8"?>
<local:SomeBaseComponent xmlns:fx="http://ns.adobe.com/mxml/2009" 
                         xmlns:s="library://ns.adobe.com/flex/spark" 
                         xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:local="*"
                         creationComplete="cc(event)">
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.events.FlexEvent;

            protected function cc(event:FlexEvent):void
            {
                mnuSource = "Hi there!";
                Alert.show(mnuSource);
            }

        ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <s:Label text="{mnuSource}" />
</local:SomeBaseComponent>