1

I've embedded an image in my Flex mxml file (script section) using:

<fx:Script>
   <![CDATA[
   import com.views.myTitleWindowFile;
   ...
   [Embed(source="com/assets/image_error.png")]
   public static const IMG_ERROR:Class;
   ...

and it works fine. Now I want to use the same image in an mxml file named myTitleWindowFile (for a spark TitleWindow component), which was imported as shown above. The myTitleWindowFile.mxml gives an error on this line:

Alert.show("Please enter a value.", "Error",Alert.OK,null,null,IMG_ERROR);

The error states: 1120: Access of undefined property IMG_ERROR. Any idea what I'm doing wrong? Thanks in advance for any comments.

ggkmath
  • 4,188
  • 23
  • 72
  • 129
  • I found one solution here: http://stackoverflow.com/questions/971355/what-is-the-best-way-to-share-assets-icons-images-across-multiple-flex-appli The idea is to create a separate class for images and call that class from any file that needs it. – ggkmath Jun 01 '12 at 21:47

1 Answers1

1

Your myTitleWindowFile shouldn't be accessing any resource in its parent. It can easily lead to a situation where you want to reuse that myTitleWindowFile in another area of your application that doesn't have IMG_ERROR defined.

I would just declare IMG_ERROR in myTitleWindowFile and access it locally. It keeps your code cleaner and more portable.

Jason Towne
  • 8,014
  • 5
  • 54
  • 69
  • Thanks Jason, How to do that while still only embedding the file once? That is, if the same icon used in the TitleWindow is also used throughout the application? – ggkmath Jun 01 '12 at 21:44
  • @ggkmath I was about to suggest something similar to the other answer you already found. I personally don't have a problem declaring the `IMG_ERROR` class in multiple components as long as they are all referencing the same source image in my `/assets/` folder. If you update the source image all of the classes that have that image embedded are going to get updated. – Jason Towne Jun 01 '12 at 22:07
  • So, then do you prefer to sacrifice file size (e.g. increase it by embedding multiple times (I assume that is what you mean by "declare")) to get the benefit of making the TitleWindow code (in this case) more portable? Or, do you avoid embedding multiple times, you just declare a similar class multiple times to keep the code portable? – ggkmath Jun 01 '12 at 22:10
  • @ggkmath Sorry, yes that is what I meant by "declare". It really depends on the situation. If file size is a concern then using a static resource class as suggested in the other answer is probably a good option especially if the images you're embedding are large themselves. The apps I've worked on in the past are very image-light and file size is not a big concern but developing components that are easily re-usable and as self-contained as possible is a big focus. – Jason Towne Jun 01 '12 at 22:15