Flex AIR编译未定义属性的错误访问

问题描述 投票:2回答:3

通过一系列Adobe AIR示例,我遇到了一个编译错误,其中一个我已经提炼到以下演示应用程序文件中

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication
  xmlns:mx="http://www.adobe.com/2006/mxml">
  <mx:Script>
    <![CDATA[
      import mx.events.MenuEvent;

      private static const MENU_DEMO:String = "Demo...";

      private function onMenuItemClick(evt:MenuEvent):void
      {
        switch(evt.label)
        {
          case MENU_DEMO:
            break;
        }
      }
    ]]>
  </mx:Script>

  <mx:VBox width="100%" height="100%" paddingBottom="5">
    <mx:MenuBar id="menuBar"
      width="100%"
      labelField="@label"
      itemClick="onMenuItemClick(event);">
      <mx:XMLList>
        <menuitem label="Error">
          <menuitem label="{MENU_DEMO}" />
        </menuitem>
      </mx:XMLList>
    </mx:MenuBar>
  </mx:VBox>
</mx:WindowedApplication>

描述符文件是哪个

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://ns.adobe.com/air/application/1.0.M6">
  <id>ErrorDemo</id>
  <filename>ErrorDemo</filename>
  <name>Error Demo</name>
  <version>v0.1</version>
  <description>Demo undefined property error</description>
  <copyright></copyright>
  <initialWindow>
    <title>Error Demo</title>
    <content>ErrorDemo.swf</content>
    <systemChrome>standard</systemChrome>
    <transparent>false</transparent>
    <visible>true</visible>
  </initialWindow>
</application>

编译产生以下输出

C:\Projects\AIR\ErrorDemo>amxmlc ErrorDemo.mxml
Loading configuration file C:\Projects\flex_sdk_4.6\frameworks\air-config.xml
C:\Projects\AIR\ErrorDemo\ErrorDemo.mxml(28):  Error: Access of undefined property _ErrorDemo_XMLList1.

          <menuitem label="{MENU_DEMO}" />

问题似乎是使用绑定到menuitem标签的label属性的静态const {MENU_DEMO},因为用文本替换它会导致没有编译错误。 Adobe的Using Flex 4.6文档指出静态常量可以用作数据绑定源,但可能不像它们在这里使用的那样。有谁知道以这种方式使用它们有什么问题?

澄清:用字符串文字{MENU_DEMO}替换绑定常量引用Demo...会产生以下预期输出。但是使用字符串文字代替绑定的常量引用会破坏使用绑定常量的目的。这似乎是产生错误的原因,也是这篇文章的重点。

ErrorDemo expected output

flex actionscript air
3个回答
1
投票

尝试在[Bindable]之前添加private static const MENU_DEMO:String = "Demo...";它变为:

[Bindable]
private static const MENU_DEMO:String = "Demo...";

0
投票

我没有使用Flex,但是从一些关于你的问题的研究我注意到了......

(1)

你的代码有:

<menuitem label="{MENU_DEMO}" />

尝试将其设置为:

<menuitem label text="{MENU_DEMO}" />

(2)

此外,因为你说itemClick="onMenuItemClick(event);"不应该由以下支持:

import mx.events.ItemClickEvent;

(3)

如果您的代码编译正确,预期结果是什么?

我不能(或不会)测试任何Flex代码,所以让我知道这是否有效或错误...

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication
  xmlns:mx="http://www.adobe.com/2006/mxml">
  <mx:Script>
    <![CDATA[
      import mx.events.MenuEvent;
      import mx.events.ItemClickEvent; //add this

      [Bindable]
      public var MENU_DEMO:String = "Demo...";

      public function onMenuItemClick(evt:MenuEvent):void //or try... (evt:MenuEvent = null):void
      {
        if (evt.label.text == MENU_DEMO) //untested
        {
          //do something here
          evt.label.text = "Changed...";
        }

      }

    ]]>
  </mx:Script>

  <mx:VBox width="100%" height="100%" paddingBottom="5">
    <mx:MenuBar id="menuBar"
      width="100%"
      labelField="@label"
      itemClick="onMenuItemClick(event);">
      <mx:XMLList xmlns="">
        <menuitem label="Error" />
        <menuitem label text="{MENU_DEMO}" />
        </menuitem>
      </mx:XMLList>
    </mx:MenuBar>
  </mx:VBox>
</mx:WindowedApplication>

0
投票

对于任何对mxml代码的Flex 4版本感兴趣的人,这是我想出的,遵循@ ProgrammerDancuk的建议,谁真的应该得到信用

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication
  xmlns:fx="http://ns.adobe.com/mxml/2009"
  xmlns:mx="library://ns.adobe.com/flex/mx"
  xmlns:s="library://ns.adobe.com/flex/spark">
  <fx:Script>
    <![CDATA[
      import mx.events.MenuEvent;

      private static const MENU_DEMO:String = "Demo...";

      private function onMenuItemClick(evt:MenuEvent):void
      {
        switch(evt.label)
        {
          case MENU_DEMO:
            break;
        }
      }
    ]]>
  </fx:Script>

  <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    <fx:XMLList id="demoMenu">
      <menuitem label="Error">
        <menuitem label="{MENU_DEMO}" />
      </menuitem>
    </fx:XMLList>
    </fx:Declarations>

  <mx:VBox width="100%" height="100%" paddingBottom="5">
    <mx:MenuBar id="menuBar"
      width="100%"
      labelField="@label"
      itemClick="onMenuItemClick(event);">
      <mx:dataProvider>
        {demoMenu}
      </mx:dataProvider>
    </mx:MenuBar>
  </mx:VBox>
</s:WindowedApplication>
© www.soinside.com 2019 - 2024. All rights reserved.