如何使用 flex 将图像作为图标动态加载到 TileList 中?

发布于 2024-07-13 08:48:04 字数 867 浏览 9 评论 0原文

好的,我创建了一个自定义渲染:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怕倦 2024-07-20 08:48:04

这不起作用的原因是 fileIcon 属性的类型是 Class。 如果您打算像工厂一样使用它来创建该类的实例,那么您通常只需要一个 Class 类型的对象。 当您使用[Embed]元数据时,您向编译器指示它应该将指定的资源嵌入到SWF中,并生成一个Class来充当工厂可以代表该资产的自动售货对象实例。 但是,正如您在发布此问题之前已经发现的那样, [Embed] 的问题是您需要对引用进行硬编码,它不允许您在运行时提供值(因为编译器需要在编译时从字面上嵌入资产)。

幸运的是,mx:Image.source 是一个非常灵活的属性,它也接受字符串(尽管大多数文档都演示了将其与嵌入资源一起使用)。 只要 Flex 应用程序能够加载资源,您就可以提供字符串类型的 URL 作为

fileIcon 的类型更改为 String,并验证 value.file.url 实际上是应用程序可以加载的图像的 URL。 (您只需将此 URL 硬编码到 mx:Imagesource 属性中即可进行测试。)

The reason that this isn't working is because the type of the fileIcon property is Class. You would generally would only want an object of type Class 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 a Class 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 the source.

Change the type of fileIcon to a String, and also verify that value.file.url is actually a URL of an image that your application can load. (You can test this just by hardcoding this URL into the mx:Image's source attribute.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文