翻译(How to pass data to Flex application)

参考地址:http://www.adobe.com/cfusion/communityengine/index.cfm?event=showdetails&productId=2&postId=12166

问题描述

Flex程序编译成SWF文件之后,你想让SWF基于外部的数据有不同的表现,或是你想显示这些数据。

解决方案

可以通过Flash vars,query String或者URL向Flex程序传参。

1. 通过Flash vars传参

在HTML wrapper中添加以键值对形式出现的Flash vars。在Flex程序中读取变量Application.application.parameters,类型是Object。比如,你在Flash vars中添加名为”key1″的key,在程序中可通过Application.application.parameters.key1访问。更多信息参考http://livedocs.adobe.com/flex/3/html/passingarguments_3.html

2. 通过query字符串传参

query字符串亦以键值对形式出现,不过现在是将该字符串添加到HTML wrapper中SWF文件的src属性后面。与Flash vars一样通过变量Application.application.parameters访问。更多信息参考http://livedocs.adobe.com/flex/3/html/passingarguments_3.html

3. 通过URL信息片段(fragment)传参

上述俩种方法在HTMl wrapper页面中完成传参过程,而通过URL fragment方式它允许你给指向嵌入SWF的HTML页面的URL传参。你所需要做的是在URL后面添加”#”,并在”#”后添加键值对形式的数据。比如你想给程序传递键名为”key1″的值”value1″,URL如下:http://www.yourdomian.com/webappcontext/yourhtml.html#key1=value1.在Flex程序中可通过BrowseManager和URLUtil类访问。BrowseManager的fragment属性即表示”#”后面的字符串,本例中fragment即为key1=value1, 然后通过URLUtil类的静态方法stringToObject可将fragment转为对象。更多信息参考http://livedocs.adobe.com/flex/3/html/deep_linking_5.html#245869

//================================================================//

以下部分摘自Adobe Livedocs

例1.直接修改template文件在HTML wrapper页面的<object>和<embed>标签中都添加flashVars。(因为浏览器兼容的原因)

<html>
<head>
<title>/flex2/code/wrapper/SimplestFlashVarTestWrapper.html</title>
<style>
body { margin: 0px;
 overflow:hidden }
</style>
</head>
<body scroll='no'>
<table width='100%' height='100%' cellspacing='0' cellpadding='0'><tr><td valign='top'>

<h1>Simplest FlashVarTest Wrapper</h1>

    <object id='mySwf' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab' height='100%' width='100%'>
        <param name='src' value='FlashVarTest.swf'/>
        <param name='flashVars' value='firstName=Nick&lastName=Danger'/>
        <embed name='mySwf' src='FlashVarTest.swf' pluginspage='http://www.adobe.com/go/getflashplayer' height='100%' width='100%' flashVars='firstName=Nick&lastName=Danger'/>
    </object>

</td></tr></table>
</body>
</html>

例2. 修改template中JS

AC_FL_RunContent(
    "src", "TestApp",
    "flashVars", "myName=Danger&myHometown=Los%20Angeles",
    "width", "100%",
    "height", "100%",
    "align", "middle",
    "id", "TestApp",
    "quality", "high",
    "name", "TestApp",
    "allowScriptAccess","sameDomain",
    "type", "application/x-shockwave-flash",
    "pluginspage", "http://www.adobe.com/go/getflashplayer"
);

例3.给swf的src属性传值

<object ... >
    <param name='src' value='TitleTest.mxml.swf?myName=Danger'>
    ...
    <embed src='TitleTest.mxml.swf?myName=Danger' ... />
</object>

上述3例在Flex程序中访问:

<?xml version="1.0" encoding="utf-8"?>
<Application xmlns="http://www.adobe.com/2006/mxml"
creationComplete="handleCreationComplete(event)">
<Script>
<![CDATA[
import mx.managers.BrowserManager;
import mx.controls.Alert;
import mx.events.FlexEvent;
private function handleCreationComplete(event:FlexEvent):void
{
BrowserManager.getInstance().init(
var param:Object = Application.application.parameters;
for (var prop:String in param)
{
trace([prop, param[prop]]);
}
}
]]>
</Script>
</Application>

例4.利用URL fragment

http://www.mydomain.com/MyApp.html#firstName=Nick;lastName=Danger
<?xml version="1.0" encoding="utf-8"?>
<!-- deeplinking/PassURLParamsAsFragments.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    layout="vertical"
    historyManagementEnabled="false"
    creationComplete="init(event);"
>
    <mx:Script>
    <![CDATA[
        import mx.managers.BrowserManager;
        import mx.managers.IBrowserManager;
        import mx.utils.URLUtil;

        private var bm:IBrowserManager;
        [Bindable]
        private var fName:String;
        [Bindable]
        private var lName:String;            

        private function init(e:Event):void {
            bm = BrowserManager.getInstance();
            bm.init("", "Welcome!");

            /* The following code will parse a URL that passes firstName and lastName as
               query string parameters after the "#" sign; for example:
               http://www.mydomain.com/MyApp.html#firstName=Nick&lastName=Danger */
            var o:Object = URLUtil.stringToObject(bm.fragment, "&");
            fName = o.firstName;
            lName = o.lastName;
        }
    ]]>
    </mx:Script>

    <mx:Form>
        <mx:FormItem label="First name:">
            <mx:Label id="ti1" text="{fName}"/>
        </mx:FormItem>
        <mx:FormItem label="Last name:">
            <mx:Label id="ti2" text="{lName}"/>
        </mx:FormItem>
    </mx:Form>

</mx:Application>
Advertisements