flex 3 将数据从模块传递到父应用程序以在视图堆栈中切换视图
大家好,stackoverflowers,
我一直在写一段代码。 我有一个带有 viewstack 的应用程序,其中加载了 5 个模块。 每个模块都是通过 moduleLoader 标签加载的,并且它们都有一个 id。
每个加载的模块都有一个上下文菜单。上下文菜单有 5 个菜单项。 视图堆栈的每个视图都有一个菜单项。
上下文菜单通过 xml 加载。
这是我的申请文件。
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
backgroundColor="#b1b1b1"
backgroundGradientColors="[#b1b1b1,#252525]">
<mx:Script>
<![CDATA[
import mx.core.Container;
//change viewstack views via modules context menu
public function switchView(viewId:String):void
{
var container:Container = Container(tops.getChildByName(viewId));
if (container != null)
{
tops.selectedChild = container;
}
}
]]>
</mx:Script>
<mx:ViewStack id="tops" width="100%" height="100%">
<mx:ModuleLoader id="admin" url="view/admin.swf" width="100%" height="100%"/>
<mx:ModuleLoader id="tv" url="view/tv.swf" width="100%" height="100%"/>
<mx:ModuleLoader id="community" url="view/community.swf" width="100%" height="100%"/>
<mx:ModuleLoader id="shop" url="view/shop.swf" width="100%" height="100%"/>
<mx:ModuleLoader id="communicator" url="view/communicator.swf" width="100%" height="100%"/>
</mx:ViewStack>
</mx:Application>
这是我的模块中的 switch 语句
public function changeView():void{
switch(action) {
case "admin":
parentApplication.switchView("admin");
break;
case "tv":
parentApplication.switchView("tv");
break;
case "shop":
parentApplication.switchView("shop");
break;
case "community":
parentApplication.switchView("community");
break;
case "default":
parentApplication.switchView("communicator");
break;
}
}
,这是我的上下文菜单 xml
<mx:XML id="appMenu">
<root>
<menuitem enabled="false"/>
<menuitem label="Administration" action="admin" icon="adminMDI"/>
<menuitem label="Television" action="tv" icon="tvMDI"/>
<menuitem label="Community" action="community" icon="communityMDI"/>
<menuitem label="Shopping Mall" action="shop" icon="shoppingMallMDI"/>
<menuitem label="Communicator" action="default" icon="communicatorMDI"/>
</root>
</mx:XML>
我想做的是通过单击上下文菜单中的菜单项之一来切换视图堆栈中的视图。 我无法从我的模块与应用程序进行通信。 我做错了什么? 我必须做什么? 有人可以帮我吗?
哦,在我忘记
上下文菜单的 xml 位于模块中之前,上下文菜单位于扩展按钮的 as 文件中。
请任何人给我一个如何实现这一目标的好例子。
感谢
DJ
Hello Fellow stackoverflowers,
I´m stuck writing a piece of code.
I have application with a viewstack witch load 5 modules.
each module is loaded via the moduleLoader tag and they all have an id.
Every loaded module has a context menu. the context menu has 5 menuItems.
one menuItem for each view for the viewstack.
The context menu is loaded via xml.
this is my application file.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
backgroundColor="#b1b1b1"
backgroundGradientColors="[#b1b1b1,#252525]">
<mx:Script>
<![CDATA[
import mx.core.Container;
//change viewstack views via modules context menu
public function switchView(viewId:String):void
{
var container:Container = Container(tops.getChildByName(viewId));
if (container != null)
{
tops.selectedChild = container;
}
}
]]>
</mx:Script>
<mx:ViewStack id="tops" width="100%" height="100%">
<mx:ModuleLoader id="admin" url="view/admin.swf" width="100%" height="100%"/>
<mx:ModuleLoader id="tv" url="view/tv.swf" width="100%" height="100%"/>
<mx:ModuleLoader id="community" url="view/community.swf" width="100%" height="100%"/>
<mx:ModuleLoader id="shop" url="view/shop.swf" width="100%" height="100%"/>
<mx:ModuleLoader id="communicator" url="view/communicator.swf" width="100%" height="100%"/>
</mx:ViewStack>
</mx:Application>
and this is my switch statement in my Module
public function changeView():void{
switch(action) {
case "admin":
parentApplication.switchView("admin");
break;
case "tv":
parentApplication.switchView("tv");
break;
case "shop":
parentApplication.switchView("shop");
break;
case "community":
parentApplication.switchView("community");
break;
case "default":
parentApplication.switchView("communicator");
break;
}
}
and this is my context menu xml
<mx:XML id="appMenu">
<root>
<menuitem enabled="false"/>
<menuitem label="Administration" action="admin" icon="adminMDI"/>
<menuitem label="Television" action="tv" icon="tvMDI"/>
<menuitem label="Community" action="community" icon="communityMDI"/>
<menuitem label="Shopping Mall" action="shop" icon="shoppingMallMDI"/>
<menuitem label="Communicator" action="default" icon="communicatorMDI"/>
</root>
</mx:XML>
What i would like to do is switch the views in the viewstack by clicking on one of the menuitems in the context menu.
i can't communicate from my module to the application.
What am i doing wrong?
what must i do?
Can anybody help me out?
Oyeah before i forget
the xml of the context menu is in the module but, the context menu is in a as file that extensiate a button.
please can any body give me a good example how to accomplish this.
Thank
DJ
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在进入多模块通信之前,我看到了几个问题。
首先,在你的changeView()函数中,你声明变量动作然后打开它。
因为您的 switch 语句中没有“默认”情况,所以永远不会调用parentApplication.switchView。
另外,为了简洁起见,您可以这样编写 switch 语句:
最后,您可以节省更多的打字时间,因为您的操作和模块 ID 相同,您可以这样做:
也许尝试这些事情,然后更新您的问题(另外,您的上下文菜单的 XML 在您的问题中未正确呈现)。这可能会帮助社区更轻松地解决您的问题。
更新
我认为问题不在于模块通信。我构建了一个简单的项目,可以满足我认为您正在寻找的功能。我已在下面发布了来源。
mmodules.mxml
interfaces/IApplication.as
views/module1.mxml
希望有帮助。
I see a couple issues before getting into the multi-module communication.
First, in your changeView() function, you are declaring the variable action and then switching on it.
Because you don't have a 'default' case in your switch statement(s), parentApplication.switchView will never be called.
Also, for the sake of brevity, you can write switch statements like this:
Finally, you could save yourself even more typing because your action and module ids are the same, you could do this:
Maybe try those things and then updating your question (also, the XML for your context menus didn't render correctly in your question). That may help the community solve your issue a little easier.
UPDATE
I don't think the problem is in the module communication. I built a simple project that does what I think you're looking for. I've posted the source below.
mmodules.mxml
interfaces/IApplication.as
views/module1.mxml
Hope that helps.