MXML和AS相互调用问题
先看看我的
- JScript code
<?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:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> <fx:Script> <![CDATA[ import com.action.Action; import mx.collections.ArrayList; import mx.events.FlexEvent; var action : Action = new Action(); ]]> </fx:Script> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <s:Panel id="cfb" x="190" y="72" width="586" height="340" title="9 9 乘法表"> <s:TextArea id="_tx" x="10" y="10" width="562" height="258"/> <s:Button x="212" y="281" width="117" label="click me" click="action.setValue()"/> </s:Panel></s:Application>
再来看看我的AS:
- Java code
package com.action{ public class Action { public var a:app; public var str:String = ""; public function Action(){ } public function setValue():void{ for(var i : Number = 1;i < 10;i++){ for(var j : Number = 1;j < i + 1;j++){ if(i * j < 10){ str+= j + " * " + i + " = " + i * j + " " + " " + " " + " "; }else{ str+= j + " * " + i + " = " + i * j + " " + " "; } } str+="\n"; } a._tx.text = str; } }}
为什么我点按钮的时候 9 9乘法表没有出现在文本域里?我的调用有什么不规范的?大虾们来帮我看看啦。。我接触FLEX不到24小时啊。。。
[解决办法]
在app中加入
[Bindable]
private var action : Action = new Action();
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
action.a = this;
}
[解决办法]
把a._tx.text = str;
换成
Application.application._tx.test=str;
记得导入mx.core.application;
[解决办法]
能出现才怪
[解决办法]
呃...
分捞不着了...
[解决办法]
非必要尽量不要把对象引入类里.
click句柄里直接给需要的对象赋值:
<fx:Script>
<![CDATA[
import com.action.Action;
var action : Action = new Action();
]]>
</fx:Script>
<s:TextArea id="_tx" x="10" y="10" width="562" height="258"/>
<s:Button x="401" y="290" width="117" label="click me" click="{_tx.text=action.setValue()}"/>
--------------------
setValue返回string
public function setValue():String{
var str:String = "";
for(var i : Number = 1;i < 10;i++){
for(var j : Number = 1;j < i + 1;j++){
if(i * j < 10){
str+= j + " * " + i + " = " + i * j + " " + " " + " " + " ";
}else{
str+= j + " * " + i + " = " + i * j + " " + " ";
}
}
str+="\n";
}
return str;
}
尽可能的剥离对象间的依赖关系.
[解决办法]
[解决办法]
支持#7的观点