从 flex 开始 - 请让我知道方向是否正确(ActionScript 与 MXML 分离)
我刚刚开始使用 OReilly“Programming Flex 3.0”学习 Flex。完成三章并开始第四章(ActionScript)后,没有足够的耐心等到完成第22章我开始练习:)
我现在最担心的一点是双重编码模式(MXML vs ActionScript)
拜托看看下面我的代码(它通过 mxmlc design.mxml 编译,第二个文件“code.as”应该位于同一目录中)并建议我在视觉设计和代码之间使用的分离是否合适。
另外 - 如果某个聪明的人可以向我展示如何使用 *. 作为类文件 [包?] 重新编码相同的示例,我将非常感激。我在为包创建目录结构时迷失了 - 并且没有发现它是最直观的,特别是对于像我的示例这样有两个文件的小项目。
代码:design.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script source="code.as"/>
<mx:VBox>
<mx:TextInput creationComplete="initializeCalculator()" id="txtScreen"/>
<mx:HBox>
<mx:Button click="click('7')" id="btn7" label="7"/>
<mx:Button click="click('8')" id="btn8" label="8"/>
<mx:Button click="click('9')" id="btn9" label="9"/>
<mx:Button click="click('C')" id="btnClear" label="C"/>
</mx:HBox>
<mx:HBox>
<mx:Button click="click('4')" id="btn4" label="4"/>
<mx:Button click="click('5')" id="btn5" label="5"/>
<mx:Button click="click('6')" id="btn6" label="6"/>
<mx:Button click="click('/')" id="btnDivide" label="/"/>
</mx:HBox>
<mx:HBox>
<mx:Button click="click('1')" id="btn1" label="1"/>
<mx:Button click="click('2')" id="btn2" label="2"/>
<mx:Button click="click('3')" id="btn3" label="3"/>
<mx:Button click="click('*')" id="btnMultiply" label="*"/>
</mx:HBox>
<mx:HBox>
<mx:Button click="click('0')" id="btn0" label="0"/>
<mx:Button click="click('=')" id="btnEqual" label="="/>
<mx:Button click="click('-')" id="btnMinus" label="-"/>
<mx:Button click="click('+')" id="btnPlus" label="+"/>
</mx:HBox>
</mx:VBox>
</mx:Application>
代码:code.as
public var res:int = 0;
public var previousOperator:String = "";
public var previousRes:int=0;
public function initializeCalculator():void{
txtScreen.text = res.toString();
}
public function click(code:String):void{
if (code=="1" || code=="2" || code=="3" || code=="4" || code=="5" ||
code=="6" || code=="7" || code=="8" || code=="9" || code=="0"){
res = res*10 + int(code);
txtScreen.text = res.toString();
}
else if (code=="C"){
res = 0;
previousOperator ="";
previousRes = 0;
txtScreen.text = res.toString();
}
else{
calculate(code);
}
}
public function calculate(operator:String):void{
var tmpRes:int;
if (previousOperator=="+"){
tmpRes = previousRes + res;
}
else if (previousOperator=="-"){
tmpRes = previousRes - res;
}
else if (previousOperator=="/"){
tmpRes = previousRes / res;
}
else if (previousOperator=="*"){
tmpRes = previousRes * res;
}
else{
tmpRes = res;
}
previousOperator = operator;
previousRes = tmpRes;
txtScreen.text = previousRes.toString();
res = 0;
if (previousOperator=="=")
{
res = tmpRes;
txtScreen.text=res.toString();
}
}
PS。如果您有意见认为此计算器无法正确计算,我们也将不胜感激,但最重要的是对 Flex 最佳实践的评论。
I've just started learning flex using OReilly "Programming Flex 3.0". After completing three chapters and starting fourth (ActionScript), and not having enough patience to wait till completing chapter 22 I started to practice :)
One bit that I have most worries about right now is the the dual coding mode (MXML vs ActionScript)
Please have a look at my code below (it compiles via mxmlc design.mxml, second file 'code.as' should be in same directory) and advise if the separation I used between visual design and code is appropriate.
Also - if some smart guy could show me how to recode same example with *.as being a class file [package?] it would be highly appreciated. I got lost with creating directory structure for package - and did not find it most intuitive, especially for small project that has two files like my example.
Code: design.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script source="code.as"/>
<mx:VBox>
<mx:TextInput creationComplete="initializeCalculator()" id="txtScreen"/>
<mx:HBox>
<mx:Button click="click('7')" id="btn7" label="7"/>
<mx:Button click="click('8')" id="btn8" label="8"/>
<mx:Button click="click('9')" id="btn9" label="9"/>
<mx:Button click="click('C')" id="btnClear" label="C"/>
</mx:HBox>
<mx:HBox>
<mx:Button click="click('4')" id="btn4" label="4"/>
<mx:Button click="click('5')" id="btn5" label="5"/>
<mx:Button click="click('6')" id="btn6" label="6"/>
<mx:Button click="click('/')" id="btnDivide" label="/"/>
</mx:HBox>
<mx:HBox>
<mx:Button click="click('1')" id="btn1" label="1"/>
<mx:Button click="click('2')" id="btn2" label="2"/>
<mx:Button click="click('3')" id="btn3" label="3"/>
<mx:Button click="click('*')" id="btnMultiply" label="*"/>
</mx:HBox>
<mx:HBox>
<mx:Button click="click('0')" id="btn0" label="0"/>
<mx:Button click="click('=')" id="btnEqual" label="="/>
<mx:Button click="click('-')" id="btnMinus" label="-"/>
<mx:Button click="click('+')" id="btnPlus" label="+"/>
</mx:HBox>
</mx:VBox>
</mx:Application>
code: code.as
public var res:int = 0;
public var previousOperator:String = "";
public var previousRes:int=0;
public function initializeCalculator():void{
txtScreen.text = res.toString();
}
public function click(code:String):void{
if (code=="1" || code=="2" || code=="3" || code=="4" || code=="5" ||
code=="6" || code=="7" || code=="8" || code=="9" || code=="0"){
res = res*10 + int(code);
txtScreen.text = res.toString();
}
else if (code=="C"){
res = 0;
previousOperator ="";
previousRes = 0;
txtScreen.text = res.toString();
}
else{
calculate(code);
}
}
public function calculate(operator:String):void{
var tmpRes:int;
if (previousOperator=="+"){
tmpRes = previousRes + res;
}
else if (previousOperator=="-"){
tmpRes = previousRes - res;
}
else if (previousOperator=="/"){
tmpRes = previousRes / res;
}
else if (previousOperator=="*"){
tmpRes = previousRes * res;
}
else{
tmpRes = res;
}
previousOperator = operator;
previousRes = tmpRes;
txtScreen.text = previousRes.toString();
res = 0;
if (previousOperator=="=")
{
res = tmpRes;
txtScreen.text=res.toString();
}
}
PS. If you have comments that this calculator does not calculate properly, they are also appreciated, yet most important are comments on best practices in Flex.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个关于在 Flex 应用程序中使用代码隐藏的示例应该会有所帮助清理用户界面和应用程序逻辑之间的链接。
This example on using code-behind in a Flex application should be helpful in cleaning up the link between your user interface and the application logic.
我通常将代码编写在
标记本身内的CDATA
块中。大多数教程和 Adobe 代码示例都是采用这种方式。 mxml 文件代表一个 ActionScript
class
(文件 design.mxml 在根包中创建一个名为design
的类),因此我相信将整个代码放入其中是有意义的一个地方(文件) - 但这是一个选择问题。如果组件中的用户界面部分很少,您可以仅使用 AS 文件创建该组件。查看带有文本输入和按钮的面板的代码:(
几乎)等效的 mxml 代码将是:
I normally write the code in a
CDATA
block inside the<mx:script/>
tag itself.This is the way it is done in most of the tutorials and Adobe code samples out there. An mxml file represents an ActionScript
class
(the file design.mxml creates a class nameddesign
in the root package) and hence I believe it makes sense to put the whole code in one place (file) - this is a matter of choice though.If user interface parts in a component are minimal, you can create that component using just an AS file. Checkout the code for a panel with a text input and a button in it:
And the (almost) equivalent mxml code would be: