Access Component Properties From Extending Component
I have a component with a 开发者_运维问答public variable declared
[Bindable]
public var mnuSource:String;
When I extend this component, I can reference mnuSource (it compiles) but Runtime complains about the property not being accessible (error 1056).
How do you modify / declare component properties so they are actually available to other components?
Thanks
Same as The_asMan but in MXML
Application
<?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>
Some Base Component
<?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>
Extended Component
<?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>
This is untested code but should work and give you a good idea how to extend a class.
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
精彩评论