Marcus Stade
Marcus Stade

Reputation: 4984

Dictionary declaration in MXML, is it possible?

In MXML it's possible to declare object instances, even for dynamic objects, like so:

<mx:Object>
    <mx:foo>bar</mx:foo>
    <mx:bar>foo</mx:bar>
</mx:Object>

Is it possible to do the same with the Dictionary class without using some MXML wrapper class?

Upvotes: 0

Views: 1535

Answers (3)

Wouter Coekaerts
Wouter Coekaerts

Reputation: 9735

It is possible, if you import the right namespace

    <mx:Application ... xmlns:utils="flash.utils.*">
        <utils:Dictionary>
            <utils:foo>bar</utils:foo>
            ...
        </utils:Dictionary>
        ...

But I see no good reason to do this. The advantage of a Dictionary over a plain Object (or <mx:Model>), what you would normally use, is that you can use objects other than String as key. But you can't write a non-String key in MXML.

In other words: yes you can create one, but there's no point.

Upvotes: 1

Christian Nunciato
Christian Nunciato

Reputation: 10409

I'm not completely sure I understand the question, but if you're asking whether you can declare a dictionary in MXML, yes, you can; here's some code demonstrating a Dictionary declared in MXML, along with a form showing how you might add items to the dictionary dynamically:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" xmlns:utils="flash.utils.*" creationComplete="onCreationComplete()">

    <mx:Script>
        <![CDATA[

            private function onCreationComplete():void
            {
                showContents();
            }

            private function onBtnClick():void
            {
                addProperty();
                showContents();
            } 

            private function showContents():void
            {
                txt.text = "";

                for (var k in d) 
                    txt.text += k + ": " + d[k] + "\n"
            }

            private function addProperty():void
            {
                d[pname.text] = pvalue.text;

                pname.text = ""; 
                pvalue.text = "";

                pname.setFocus();
            }

        ]]>
    </mx:Script>

    <mx:VBox>
        <mx:Label text="Dictionary Contents" />
        <mx:TextArea id="txt" width="350" height="200" />
        <mx:HBox>
            <mx:VBox>
                <mx:Label text="New Property Name" />
                <mx:TextInput id="pname" text="pets" />
            </mx:VBox>
            <mx:VBox>
                <mx:Label text="New Property Value" />
                <mx:TextInput id="pvalue" text="dog, cat, fish" />
            </mx:VBox>
        </mx:HBox>
        <mx:Button id="btn" label="Add" click="onBtnClick()" />
    </mx:VBox>

    <utils:Dictionary id="d">
        <utils:fname>Chris</utils:fname>
        <utils:lname>Nunciato</utils:lname>
    </utils:Dictionary>

</mx:Application>

Hope that helps! If I've totally missed the point, post back and I'll see if I can help out.

Upvotes: 6

CookieOfFortune
CookieOfFortune

Reputation: 13994

No, I don't think Dictionary is bound to MXML.

Do you really need a Dictionary for what you want to accomplish?

Upvotes: 0

Related Questions