我一直在努力研究如何从应用程序中创建的显示列表构建 mxml。例如,在画布上创建多个组件,然后单击按钮以获取画布及其属性的格式化 xml 文档(以 mxml 格式)。
有一个简单的方法吗?
非常感谢您的帮助...
我一直在努力研究如何从应用程序中创建的显示列表构建 mxml。例如,在画布上创建多个组件,然后单击按钮以获取画布及其属性的格式化 xml 文档(以 mxml 格式)。
有一个简单的方法吗?
非常感谢您的帮助...
肯定没有简单的方法。
编辑:这背后的原因是mxml实际上被翻译成actionscript,然后编译。因此,flash player 对 mxml 及其存在一无所知。
了解更多有关这方面的背景会有所帮助,例如为什么需要这样做。据我所知,没有一种简单或内置的方法可以做到这一点。
这是一个可能有帮助的方向:
为您的应用将支持的每种类型的 DisplayObject 嵌入纯文本 mxml 模板文件。(模板文件,其中每个属性都有一个变量与字符串插值交换 ex:mx:HRule height="{$hRuleHeight}" width="${hRuleWidth}"/
每次编辑/创建对象时保存信息---当需要生成 mxml 时,对每个保存的项目进行排序并获取属性并使用它们解析模板。AS3 不支持字符串插值,如果你自己没有,下面是一个实现。
用法:
var example:StringInterpolation=new StringInterpolation();
example.setKeyAndValue('${id}','foo');
example.setKeyAndValue('${baseurl}','http://testing123');
trace(example.eval('<table width="480" border="0" cellpadding="0" cellspacing="0"><tr><td colspan="3"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="480" height="270"><param name="movie" value="http://testing123/player/Take180Player.swf?xmlLocation=/s/bx/http://testing123}&links=true" />'));
/*outputs:<table width="480" border="0" cellpadding="0" cellspacing="0"><tr><td colspan="3"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="480" height="270"><param name="movie" value="http://testing123/player/Take180Player.swf?xmlLocation=/s/bx/http://testing123&links=true" /> */
源代码:
//
package com.philipbroadway.utils
{
public class StringInterpolation extends Object
{
internal var _keys:Array;
internal var _values:Array;
internal var _result:String;
internal var i:uint;
/**
*
*
*/
public function StringInterpolation()
{
_keys=[];
_values=[];
}
/**
*
* @param key:String a variable name
* @param value:String a value to replace when the key is found during eval
*
*/
public function setKeyAndValue(key:String,value:String):void
{
var metacharacters:Array=[];
metacharacters.push(new RegExp(/\$/));
metacharacters.push(new RegExp(/\{/));
metacharacters.push(new RegExp(/\}/));
metacharacters.push(new RegExp(/\^/));
metacharacters.push(new RegExp(/\./));
metacharacters.push(new RegExp(/\*/));
metacharacters.push(new RegExp(/\+/));
var replacements:Array=[];
replacements.push(new String('\\$'));
replacements.push(new String('\\{'));
replacements.push(new String('\\}'));
replacements.push(new String('\\^'));
replacements.push(new String('\\.'));
replacements.push(new String('\\*'));
replacements.push(new String('\\+'));
for(i=0;i<metacharacters.length;i++)
{
key=key.replace(metacharacters[i],replacements[i]);
}
_keys.push(key);
_values.push(value);
}
/**
*
* @param value:String to be interpolated
* @return String interpolated
*
*/
public function eval(value:String):String
{
_result=value;
for(i=0;i<_keys.length;i++)
{
var regEx:RegExp=new RegExp(_keys[i],'g');
_result=_result.replace(regEx,_values[i]);
}
return _result;
}
/**
*
*
*/
public function reset():void
{
_keys=[];
_values=[];
}
}
}