Set the icon of a Flex button with a Sprite in runtime

流过昼夜 提交于 2019-12-11 06:34:22

问题


I want to set the icon of a button in flex.

The default syntax is as follows.

myButton.setStyle("icon", iconClass);

and iconClass is normally an embedded object.

But what I want to do is, use a standard Sprite or a MovieClip (which I find during runtime) as the icon.

Is this possible? Has anyone done this?

Thanks!


回答1:


This is actually a fatal flaw in Flex's framework in regards to styling. When Flex grabs a style value for an icon it assumes it's of type Class and that the instantiated object will be of type DisplayObject (or derived). It's a trivial change to the code (which classes like mx:Image do for their source property) to test whether or not the style's value is of type DisplayObject and if so, just skip the construction step that it currently performs.

Ben's solution is the best possible if you won't want to change the Flex framework source. Personally, I end up monkey-patching the specific components to accept instantiated DisplayObjects instead of Classes. Some components are more of a pain than others to patch.

Doug McCune explaining how to monkey patch the Flex framework.




回答2:


Found a possible solution. http://blog.benstucki.net/?p=42 Will give this ago and report.



来源:https://stackoverflow.com/questions/647910/set-the-icon-of-a-flex-button-with-a-sprite-in-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!