如何使用 flex 将图像作为图标动态加载到 TileList 中?
好的,我创建了一个自定义渲染:
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"
horizontalAlign="center"
verticalAlign="middle"
width="100"
height="100">
<mx:Script>
<![CDATA[
[Bindable]
private var fileLabel:String;
[Bindable]
private var fileIcon:Class;
override public function set data(value:Object):void{
fileLabel = value.label;
fileIcon = value.file.url;
}
]]>
</mx:Script>
<mx:Image source="{fileIcon}" />
<mx:Label text="{fileLabel}" />
</mx:VBox>
我想将其用于照片库,其中的图像被拖放到 TileList 上。 我已经把那部分写下来了,但我似乎无法让图标工作。
给定: value 是 File 类的包装器。 我想将 mx:Image 源设置为需要为 Class 类型的内容。 使用 nativePath 或 url 会出现强制转换错误。 我在网上看到大量使用 XML 和“Embed(/url/to/img.jpg)”之类的示例。 我向你保证,如果你给我其中一个例子(使用静态图像),我会给你投反对票。 这不是我在这里寻找的!
Ok, so I have a custom render that I have created:
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"
horizontalAlign="center"
verticalAlign="middle"
width="100"
height="100">
<mx:Script>
<![CDATA[
[Bindable]
private var fileLabel:String;
[Bindable]
private var fileIcon:Class;
override public function set data(value:Object):void{
fileLabel = value.label;
fileIcon = value.file.url;
}
]]>
</mx:Script>
<mx:Image source="{fileIcon}" />
<mx:Label text="{fileLabel}" />
</mx:VBox>
That I want to use for a photo gallery with images that are dragged and dropped onto a TileList. I have that part down, but I can't seem to get the icon thing to work.
Given: value is sort of wrapper for a File class. I want to set the mx:Image source to something that needs to be of type Class. Using nativePath or url gives me a cast error. I see tons of examples online using XML and something like "Embed(/url/to/img.jpg)". I promise you that if you give me one of those examples (using a static image) I will give you a negative vote. THAT IS NOT WHAT IM LOOKING FOR HERE!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不起作用的原因是
fileIcon
属性的类型是Class
。 如果您打算像工厂一样使用它来创建该类的实例,那么您通常只需要一个Class
类型的对象。 当您使用[Embed]
元数据时,您向编译器指示它应该将指定的资源嵌入到SWF中,并生成一个Class
来充当工厂可以代表该资产的自动售货对象实例。 但是,正如您在发布此问题之前已经发现的那样,[Embed]
的问题是您需要对引用进行硬编码,它不允许您在运行时提供值(因为编译器需要在编译时从字面上嵌入资产)。幸运的是,
mx:Image.source
是一个非常灵活的属性,它也接受字符串(尽管大多数文档都演示了将其与嵌入资源一起使用)。 只要 Flex 应用程序能够加载资源,您就可以提供字符串类型的 URL 作为源
。将
fileIcon
的类型更改为 String,并验证value.file.url
实际上是应用程序可以加载的图像的 URL。 (您只需将此 URL 硬编码到mx:Image
的source
属性中即可进行测试。)The reason that this isn't working is because the type of the
fileIcon
property isClass
. You would generally would only want an object of typeClass
if you plan to use it like a factory, creating instances of that class with it. When you use the[Embed]
metadata, you are indicating to the compiler that it should embed the specified asset into the SWF and also generate aClass
to act as a factory for vending object instances that can represent that asset. However, as you had already discovered before posting this question, the problem with[Embed]
is that you need to hard-code the reference, it doesn't let you supply a value at runtime (because the compiler needs to literally embed the asset, at compile-time).Fortunately,
mx:Image.source
is a very flexible property that also accepts Strings (despite the fact that most documentation demonstrates using it with embedded assets). As long as the Flex application is capable of loading the asset, you can just supply a String-typed URL as thesource
.Change the type of
fileIcon
to a String, and also verify thatvalue.file.url
is actually a URL of an image that your application can load. (You can test this just by hardcoding this URL into themx:Image
'ssource
attribute.)