MXML 类的子元素未出现
我正在开发一个简单的任务/日历工具,可以将任务动态添加到画布中。这是 Main.mxml 中定义的主应用程序,它本质上只是一个 Canvas 和一个用于添加任务的按钮:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Canvas id="MainCanvas" borderStyle="solid" width="300" height="300">
<mx:Button click="CreateTask();" label="create task" />
</mx:Canvas>
<mx:Script>
<![CDATA[
import Task;
private function CreateTask( ) : void
{
// create the task object
var thisTask:Task = new Task();
// add the task to the canvas
var taskUI : DisplayObject = MainCanvas.addChild( thisTask );
// position the task ui
taskUI.y = 50;
}
]]>
</mx:Script>
</mx:Application>
我希望我的任务是带有标签和按钮的 BorderContainers,在外部 mxml 中定义,可以简单地是在 Main.mxml 中实例化并添加到画布。这是 Tasks.mxml:
<?xml version="1.0" encoding="utf-8"?>
<s:BorderContainer xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:s="library://ns.adobe.com/flex/spark" cornerRadius="10">
<s:layout>
<s:HorizontalLayout/>
</s:layout>
<s:Label id="NameLabel" text="task name" />
<s:Button label="Button 1"/>
</s:BorderContainer>
问题是,当我将任务实例添加到画布时,子项(按钮和标签)不会出现。我尝试在 Canvas 和 BorderContainer 上设置creationPolicy =“all”,但它仍然不起作用。我读过很多关于人们在类完全加载之前访问类成员时遇到问题的帖子,但我想做的就是看到 Label 和 Button 显示在 BorderContainer 内。
谢谢。
I'm working on a simple task/calendar tool where tasks can be added dynamically to a canvas. Here is the main application as defined in Main.mxml, which is essentially just a Canvas and a button for adding a task:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Canvas id="MainCanvas" borderStyle="solid" width="300" height="300">
<mx:Button click="CreateTask();" label="create task" />
</mx:Canvas>
<mx:Script>
<![CDATA[
import Task;
private function CreateTask( ) : void
{
// create the task object
var thisTask:Task = new Task();
// add the task to the canvas
var taskUI : DisplayObject = MainCanvas.addChild( thisTask );
// position the task ui
taskUI.y = 50;
}
]]>
</mx:Script>
</mx:Application>
I want my tasks to be BorderContainers with a label and a button, defined in an external mxml, that can simply be instantiated and added to the canvas in Main.mxml. Here is Tasks.mxml:
<?xml version="1.0" encoding="utf-8"?>
<s:BorderContainer xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:s="library://ns.adobe.com/flex/spark" cornerRadius="10">
<s:layout>
<s:HorizontalLayout/>
</s:layout>
<s:Label id="NameLabel" text="task name" />
<s:Button label="Button 1"/>
</s:BorderContainer>
The problem is that when I add a Task instance to the Canvas, the children (the button and label) don't appear. I tried setting creationPolicy="all" on both the Canvas and the BorderContainer, but it still didn't work. I've read a bunch of posts about people having issues accessing members of their class before the class is fully loading, but all I want to do is SEE that Label and Button show up inside the BorderContainer.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想将 mx 和 Spark 控件混合在一起,您需要从 flex 3 mx 命名空间移至较新的命名空间。
这是您的 main.mxml ,其中包含新的 mx 命名空间 (library://ns.adobe.com/flex/mx),并且添加了 fx 命名空间来处理脚本块:
尽管正如其他人评论的那样,您可以将所有内容移过去激发 - 如果您这样做,请务必将 addChild 调用更改为 addElement。
You need to move off of the flex 3 mx namespace to the newer namespace if you want to mix mx and spark controls together.
Here's your main.mxml with the new mx namespace (library://ns.adobe.com/flex/mx), and the fx namespace added to handle the script block:
Although as other people have commented, you could just move everything over to spark - if you do that, be sure to change the addChild call to addElement.
看来,将 Spark 组件添加到 mx:canvas 永远不会触发创建 Spark 组件的子组件所需的代码。这是一个适合您的解决方法:
It appears that adding spark component to an mx:canvas never triggers the code necessary to create the spark component's children. Here is a workaround for you: