如何在Flex 3中调用ActionScript3.0中的MXML类
我有一个由自定义组件组成的页面。 在该页面中我有一个按钮。 如果我单击该按钮,我必须调用另一个页面(由自定义组件组成的 page.mxml)。 然后单击事件处理程序用动作脚本编写在单独的文件中。
如何在 ActionScript 中创建 MXML 类的对象? 如何显示对象(即页面)?
我的代码:
page1.mxml
<comp:BackgroundButton x="947" y="12" width="61" height="22"
paddingLeft="2" paddingRight="2" label="logout" id="logout"
click="controllers.AdminSession.logout()"
/>
此 page1.mxml 必须使用另一个类中的 ActionScript 代码调用 page2.mxml:
static public function logout():void {
var startPage:StartSplashPage = new StartSplashPage();
}
I have a page made of custom components. In that page I have a button. If I click the button I have to call another page (page.mxml consisting of custom components). Then click event handler is written in Action-script, in a separate file.
How to make a object of an MXML class, in ActionScript? How to display the object (i.e. the page)?
My code:
page1.mxml
<comp:BackgroundButton x="947" y="12" width="61" height="22"
paddingLeft="2" paddingRight="2" label="logout" id="logout"
click="controllers.AdminSession.logout()"
/>
This page1.mxml has to call page2.mxml using ActionScript code in another class:
static public function logout():void {
var startPage:StartSplashPage = new StartSplashPage();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 Actionscript 类需要对显示列表的引用,以便将组件添加到舞台。 MXML 只是声明性动作脚本,因此在 Actionscript 中创建实例或使用 MXML 表示法创建实例没有区别。
您的功能:
可以更改为:
或:
如果您的动作脚本没有对显示列表的引用,则您无法将自定义组件添加到显示列表。 添加基于 MXML 的自定义组件与将任何其他 DisplayObject 添加到显示列表没有什么不同:
等同于:
Sprite 和 StartSplashPage 都是 DisplayObject 核心的扩展。
您在另一个答案的评论中引用了 MVC。 如果不知道您已实现的具体框架,或者根据您尝试执行此操作的上下文向我们提供更多代码,则很难给出更具体的答案。
Your Actionscript class needs a reference to the display list in order to add your component to the stage. MXML is simply declarative actionscript, so there is no difference between creating your instance in Actionscript or using the MXML notation.
your function:
could be changed to:
or:
If your actionscript does not have a reference to the display list, than you cannot add the custom component to the display list. Adding an MXML based custom component is no different than adding ANY other DisplayObject to the display list:
is the same as:
Both the Sprite and the StartSplashPage are extensions of DisplayObject at their core.
You reference MVC in the comments to another answer. Without knowing the specific framework you've implemented, or providing us with more code in terms of the context you are trying to perform this action in, it is difficult to give a more specific answer.
我假设您所在的页面包含一组组件,并且想要用另一组组件替换页面上的这组组件。 如果这不是您想要做的,我提前表示歉意。
您可以使用 ViewStacks 并在选择时切换选定的索引来完成此操作 - 这可以通过数据绑定或通过在controllers.AdminSession.logout()中触发事件并在主页中侦听该事件并切换的 selectedIndex 来完成在处理函数中查看堆栈。
主页.mxml
I assume that you are on a page with a set of components and want to replace this set of components on the page with a different set of components. My apologies in advance if this is not what you are trying to do.
You can do this using ViewStacks and switching the selected index on selection -- this can be done either by databinding or by firing an event in controllers.AdminSession.logout() and listening for that event in the Main Page and switching the selectedIndex of the view stack in the handler function.
MainPage.mxml
我认为你可以使用状态来完成你的工作。
您可以查看 http://blog.flexexamples.com/2007/10/05/creating-view-states-in-a-flex-application/#more-221
编辑:
我不确定我是否完全理解你的情况。
据我所知,您可以在 page1.mxml 中创建一个新状态,并将其命名,例如。 secondaryPageState,然后将自定义组件page2.mxml放入secondPageState中。
在控制器中,您需要一个 import 语句来导入 page1 组件并为 page1 组件创建一个公共变量,例如: 第一页。
然后,代码将类似于:
公共函数注销():无效
{
第一页.currentState = "第二页状态";
:
另一个解决方案
如果您不喜欢更改状态解决方案,您可以尝试使用 addchild 将自定义组件添加到您的应用程序中。
I think you may use state to do you work.
You may take a look at http://blog.flexexamples.com/2007/10/05/creating-view-states-in-a-flex-application/#more-221
Edit:
I am not sure I fully understand your case.
As I know, you may make a new state in page1.mxml, and name it, eg. secondPageState, and then put the custom component page2.mxml in the secondPageState.
In the controller, you need an import statement to import the page1 component and make a public var for the page1 component, eg. firstPage.
Then, the code will similar to:
public function logout():voild
{
firstPage.currentState = "secondPageState";
}
Another solution:
If you don't like the change state solution, you may try to use the addchild, to add the custom component to your application.