PDA

View Full Version : mxml extending custom .as class that extends UIComponent


Rob@ASF
03-28-2008, 07:23 PM
Hey all,

I've been trying to resolve this issue I am having but all I can find by googling is the same cookie-cutter examples.

What I am doing is simple, I am creating a custom dialog window that will sit inside of my application. The window draws a certain background and adds some title bar buttons, like close, maximize etc. (more specific than that, but just for examples sake).

This .as file is my 'basewindow' class which then gets extended by several different mxml files. These are implemented by my application. What I am finding is that any buttons, etc. that are populated in the original actionscript class showup in the component, but the new buttons I add to my mxml class do not. I am not sure if this is a layout issue, an in heritance issue, or what...

Anyone with advice? Code snippets follow....

APPLICATION:

<mx:Application .....>
...
..
<dw:TestWindow width="300" height="300" left="20" top="20" />
</mx:Application>

Where TestWindow is my mxml class that extends the .as baseclass.

This file is TestWindow.mxml:
<?xml version="1.0" encoding="utf-8"?>
<DraggableWindow xmlns="components.draggablewindow.*"
xmlns:mx="http://www.adobe.com/2006/mxml">

<mx:Button label="Test Label" x="0" y="0" width="100" height="25"/>
</DraggableWindow>


And finally the base class (drastically shortened for context):
package components.draggablewindow
{
//imports go here

public class DraggableWindow extends UIComponent
{
private var closeButton:Button;
override protected function createChildren(): void {
//add buttons - design dictates that the window will always
//have proper size to show them.
closeButton = new Button();
closeButton.move(this.width - 15, 1);
closeButton.width = 13;
closeButton.height = 13;
closeButton.setStyle("icon",closeIcon);
closeButton.addEventListener(MouseEvent.CLICK, onCloseButton);
addChild(closeButton);
super.createChildren();
}
//Usual overridden methods.
}
}

I am sure this is a basic conceptual issue that I am just not recognizing due to my lack of experience with Flex 2.0/AS 3.0, anyone able to spot my mistake? All i see when I run it, is the base DraggableWindow component shows up, without any of the additional button(s) I've added in my mxml class. I am just using inheritance improperly, or...?

Thanks in advance,
Rob

Rob@ASF
03-29-2008, 02:09 PM
As a follow-up, I gave up on the mxml sub-class and created 1 .as class that extends my DraggableWindow, and this class populates all of it's UI elements using AS code.

When I do this, i can see both components from the parent class and the child class... but now my mouse listeners won't kick in. I had created it so that when you click on a certain area you can drag the window around...but the dragging doesn't seem to want to happen now! It works perfectly if I add the Draggable Window class to my mxml Application directly, but my sub-class doesn't seem to want to work.

What am I missing about extending components? There seems to be a lot of problems with it, is it just me or AS/Flex weirdness?

Rob


*** EDIT:
I created two mxml classes, the first extending the second. Both have a test <mx:Button> item in them. When I added the child mxml component to a test Application, I got the error:
Error: Multiple sets of visual children have been specified for this component (base component definition and derived component definition).

So you can't have visual components on more than 1 file in an extension hierarchy???

Coming from a Java background, am I wrong in thinking how stupid a restriction that is? Is there anyway to do what it is I am trying?

Rob

Rob@ASF
03-29-2008, 05:41 PM
So I finally sorted out 'most' of my issues.

As for extending the mxml files, that appears to be a known issue with a very wonky work around... I just skipped that and re-wrote my child class as an .as file. It's not what I would have preferred, but at least it's done!

As for the mouse events issue, I never noticed that I was using left/right/top/bottom instead of x/y. When you use those, it ignores movement of the component apparently.

Nifty!

kahuja
03-31-2008, 03:23 AM
You could have tried (re)using the TitleWindow that has most of the propoerties that you are trying to do - a lot less code for a lot of simplicity.

To your point - I was trying to find out the better of the both worlds (AS v/s MXML) when it comes to creating components and realized that with some who likes to work with OO AS is a better option as IDE then show the variables/methods from base classes. But, the MXML also allows you to do whatever you want to do, just it will not display the hierarchy - makes things a little tougher. I still prefer to use the MXMLwhen it comes to working with multiple UIComponents.