Flex 4:有没有简单的方法来扩展 Spark 按钮?
我通过执行“文件”>“创建了一个自定义 Spark 按钮”新的> MXML 皮肤并基于spark.components.button。问题是我需要向按钮组件添加一个额外的文本字段并动态更改该文本...但是,当然,Spark 按钮无法识别该属性。
有没有一种简单的方法可以将此字段添加到我的自定义按钮皮肤和按钮皮肤中?它的属性可以解决吗?如果没有,是否有一种简单的方法可以采用我所做的事情并扩展 Spark 按钮?我似乎无法找到任何示例来说明如何在不将其全部写在 ActionScript 中的情况下执行此操作。
I created a somewhat custom Spark button by doing the File > New > MXML skin and basing it on spark.components.button. The problem is that I need to add an extra text field to the button component and dynamically change that text...but of course, the property isn't recognized on a Spark Button.
Is there a simple way to add this field to my custom button skin & its property so it can be addressed? If not, is there a simple way to take what I've done and just extend the Spark Button? I can't seem to find any examples that show how to do it without writing it all up in ActionScript.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我很高兴你问了!这比您想象的要容易得多,所以不要灰心!一旦掌握了 ActionScript 的窍门,它就会变得非常简单。
首先,让我们定义我们想要什么。读完你的问题后,我相信你会想使用你的按钮,如下所示:
所以让我们来看看如何实现这一点。
现在,我强烈建议使用 ActionScript 扩展 Button,但也可以在 mxml 中执行:
然后您可以在
中添加所需的
SkinPart
。 code>:所以现在当您制作皮肤时,您应该包含类似于原始标签的内容,只是使用不同的 ID 来反映您的新 SkinPart:
但是等等!我们的第二个标签应该显示什么文本?好吧,我们需要添加另一个可以为按钮的每个单独实例设置的属性:
很酷,所以现在我们可以在使用按钮时设置 label2,但此时它不会更改标签的实际文本属性。我们需要将 label2 连接到 secondaryLabelDisplay。我们通过在 label2 更改时调用 invalidateProperties 来实现此目的,然后更改 commitProperties 中的标签(由于 invalidateProperties() 调用而调用该标签):
最后,您会注意到,如果您再次更改
label2
运行后,标签将显示更改。但如果您像我们的目标用法一样设置初始label2
,则不会显示更改。 Flex 团队专门针对这种情况制定了一个特殊的方法,partAdded()。我不会详细介绍它,因为已经有大量关于该主题的文献。最后,这是我们完成的自定义按钮,等待皮肤使用:
祝你好运!
I'm glad you asked! This is much easier than you'd think so don't be discouraged! ActionScript is pretty easy once you get the hang of it.
First of all, let's define what we want. After reading your question I believe you would like to use your button something like this:
So let's go over how to make that a reality.
Now, I would highly suggest extending Button with ActionScript, but it is also possible to do in mxml:
Then you can add the
SkinPart
s you need in a<fx:Script>
:So now when you make a skin you should include something like the original label, just with a different ID to reflect your new SkinPart:
But wait! What text should our second label show?? Well, we will need to add another property that you can set for each individual instance of the button:
Cool, so now we can set label2 when we use our button, but at this point it won't change the label's actual text property. We need to hook up our label2 to our secondLabelDisplay. We do this by calling invalidateProperties when the label2 changes and then change the label in commitProperties (which will be called because of the invalidateProperties() call):
Lastly, you'll notice that if you change
label2
again afte runtime, the label will show the change. But it won't show the change if you set an initiallabel2
like in our target usage. The Flex team made a special method just for this case, partAdded(). I won't go over too many details about it because there is already a good amount of literature on the subject.Finally, here's our finished, custom button awaiting a skin to put it to use:
Best of luck!