[英]flex 3 accessing main mxml from actionscript code
im writting an actionScript class to handle my web service calls. 我正在编写一个ActionScript类来处理我的Web服务调用。 When i retrieve a result i want to call a setter method in my main mxml application.
当我检索结果时,我想在我的主mxml应用程序中调用setter方法。 My problem is that i dont know how to access the methods in the actionScript section of my main mxml class from my actionscript class, any ideas?
我的问题是,我不知道如何从我的actionscript类,任何想法访问我的主mxml类的actionScript部分中的方法?
David is right -- while you can access the public members of your Application.mxml object statically and from anywhere in your application, design-wise that's a bit of a no-no. David是对的 - 虽然您可以静态地从应用程序的任何位置访问Application.mxml对象的公共成员,但设计方面有点禁止。 It's better to strive for loose coupling between your objects, and the way that's done in the Flex idiom is generally to extend EventDispatcher and to dispatch events.
最好是争取对象之间的松散耦合,而在Flex习惯用法中完成的方式通常是扩展EventDispatcher和分派事件。 So for example, your WebService wrapper might look something like this:
例如,您的WebService包装器可能如下所示:
public class MyWrapperClass extends EventDispatcher
{
[Event(name="webserviceComplete", type="flash.events.Event")]
public function MyWrapperClass(target:IEventDispatcher=null)
{
super(target);
}
private function handleWebServiceLoadComplete(event:ResultEvent):void
{
dispatchEvent(new Event("webserviceComplete"));
}
public function doWork():void
{
// Load the service, etc., and ultimately call handleWebServiceLoadComplete()...
}
}
... and your Main.mxml file like this: ...和您的Main.mxml文件如下:
<mx:Script>
<![CDATA[
private function app_creationComplete(event:Event):void
{
var myWrapper:MyWrapperClass = new MyWrapperClass();
myWrapper.addEventListener("webserviceComplete", mywrapper_webServiceComplete, false, 0, true);
myWrapper.doWork();
}
private function mywrapper_webServiceComplete(event:Event):void
{
// Do the work you would've otherwise done in the public method
}
]]>
</mx:Script>
In this case, the end result is the same -- completing the web-service load triggers the function in Main.mxml. 在这种情况下,最终结果是相同的 - 完成Web服务加载会触发Main.mxml中的函数。 But notice how
mywrapper_webServiceComplete()
is declared privately -- it's not called directly by MyWrapperClass
. 但请注意
mywrapper_webServiceComplete()
是如何mywrapper_webServiceComplete()
声明的 - 它不是由MyWrapperClass
直接MyWrapperClass
。 Main.mxml simply subscribes (with addEventListener()
) to be notified when MyWrapperClass is finished doing its work, and then does its own work; Main.mxml只是订阅(使用
addEventListener()
),以便在MyWrapperClass完成其工作时得到通知,然后自己完成工作; MyWrapperClass knows nothing about the details of Main.mxml's implementation, nor does Main.mxml know anything about MyWrapperClass other than that it dispatches a webserviceComplete
event, and exposes a public doWork()
method. MyWrapperClass对Main.mxml的实现细节一无所知,除了调度
webserviceComplete
事件外,Main.mxml也不了解MyWrapperClass,并公开了一个公共的doWork()
方法。 Loose coupling and information hiding in action. 松散的耦合和信息隐藏在行动中。
Good luck! 祝好运!
If your class is an UIComponent added to the component tree, then you can use its parentApplication
attribute. 如果您的类是添加到组件树的UIComponent,那么您可以使用其
parentApplication
属性。 Otherwise, use the static Application.application
attribute, but only after the application initialization has completed. 否则,请使用静态
Application.application
属性,但仅在应用程序初始化完成后使用。 Earlier than that, the field is null
. 早于此,该字段为
null
。 Private fields and methods obviously cannot be accessed. 显然无法访问私有字段和方法。 Elements declared in the MXML part with explicit
id
s are public. 在具有显式
id
的MXML部分中声明的元素是公共的。
Adding such a call creates a rigid binding, though. 但是,添加此类调用会创建严格的绑定。 You might want to consider dispatching an event instead, and handling this event in the main application.
您可能需要考虑调度事件,并在主应用程序中处理此事件。
In case anyone has the same problem: 万一有人有同样的问题:
mx.core.FlexGlobals.topLevelApplication.YOUR_FUNCTION mx.core.FlexGlobals.topLevelApplication.YOUR_FUNCTION
is the syntax to access public functions within the main.mxml. 是访问main.mxml中的公共函数的语法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.