【问题标题】:Access Component Properties From Extending Component从扩展组件访问组件属性
【发布时间】:2011-07-29 16:15:12
【问题描述】:

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

[Bindable]
public var mnuSource:String;

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

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

谢谢

【问题讨论】:

  • 你能发布更多原始和扩展组件的代码吗?您可以跳过该代码中不相关的细节。
  • 你做对了;但显示完整的错误以及从您的子组件中抛出它的行。

标签: apache-flex actionscript-3 flex4


【解决方案1】:

与 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>

【讨论】:

    【解决方案2】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 2012-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-26
      相关资源
      最近更新 更多