在单独的组件文件中绑定到textarea.text



我有一个带有tabnavigator和textarea的创建字符组件。在我的主应用程序中,我想将文本区域id-mainchar文本保存到标签中。我尝试了点表示法,什么都没有,我尝试了导入组件。CreateCharacter;但仍然一无所获,并尝试了网上的其他选择,但无法实现。

注意,如果我调用主应用程序中的组件,代码会很好地工作,所以代码会很正常——它只是调用组件文件夹中的组件(CreateCharacters)。components文件夹位于src文件夹中。这是代码:

Main.mxml
<fx:Script>
        <![CDATA[
            import components.CreateCharacters
            [Bindable]
                public var xmlData:XML=<ROOTS></ROOTS>;
            public function sav_clickHandler(event:MouseEvent):void
            {
                    var fr:FileReference = new FileReference(); 
                    var ba:ByteArray = new ByteArray();
                    var newXmlRow:XML=<ROOTS>
                    <TXT>{components.CreateCharacters.mainchar.text}</TXT>// The problem lies with this line
                    <TXTA>{txt2.text}</TXTA>
                    <DTF>{txt3.text}</DTF>
                    </ROOTS>;
                    ba.writeMultiByte(newXmlRow, 'utf-8');
                    fr.save(ba);
                }
            private var openedFile:File;
            private function open_clickHandler(event:MouseEvent):void {
                openedFile = new File();
                openedFile.addEventListener(Event.SELECT, file_select);
                openedFile.browseForOpen("Please select a file...");
            }
            private function file_select(event:Event):void {
                if(openedFile != null && openedFile.exists){
                    var fileStream:FileStream = new FileStream();
                    fileStream.open(openedFile, FileMode.READ);
                    var readXML:XML = XML(fileStream.readUTFBytes(fileStream.bytesAvailable));
                    fileStream.close();
                    trace(readXML.toString());
                    CreateCaracters.maichar.text = readXML.TXT;
                    txt2.text = readXML.TXTA;
                    txt3.text = readXML.DTF;
                }
                trace(event);
            }

        ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
        <net:FileReference id="fileReference" />
    </fx:Declarations>
    <s:Image x="0" y="0" width="100%" height="50" scaleMode="stretch"
             source="assets/imaginationFly.png"/>
    <s:Image x="12" y="1" source="assets/ECWDove.png"/>
    <mx:TabNavigator x="1" y="49" width="100%" height="100%" backgroundColor="#7EB7C5"
                     chromeColor="#85B5BF">
        <s:NavigatorContent width="100%" height="100%" label="Start">
            <s:TextArea id="txt2" x="57" y="29"/>
        </s:NavigatorContent>
        <s:NavigatorContent id="Characters" width="100%" height="100%" label="Characters">
        <components:CreateCharacters id="creatchr" width="100%" height="100%"/>
        </s:NavigatorContent>
        <s:NavigatorContent width="100%" height="100%" label="Worlds">
            <s:TextArea id="txt3" x="55" y="10"
                        text="hello&#xd;I am testing this shit&#xd;hope it works"/>
            <s:Button id="sav" x="285" y="138" label="Save" click="sav_clickHandler(event)"/>
            <s:Button id="open" x="381" y="138" label="Open" click="open"/>

等等。。。

CreateCharaters.mxml(组件)

<?xml version="1.0" encoding="utf-8"?>
<s:HGroup xmlns:fx="http://ns.adobe.com/mxml/2009"
          xmlns:s="library://ns.adobe.com/flex/spark"
          xmlns:mx="library://ns.adobe.com/flex/mx"
          chromeColor="#0106BD" paddingLeft="10" paddingRight="0">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <mx:ViewStack id="Characters" x="172" y="10" width="81" height="100%" backgroundColor="#030BB3"
                  chromeColor="#D7D7D8" paddingLeft="5" paddingRight="15">

        <s:NavigatorContent id="Hero" width="100%" height="95%" label="Main Charater">
            <s:HGroup width="100%" height="100%">
                <s:TextArea id="mainchar" bottom="0" width="80%" height="100%"
                            chromeColor="#7070FD">//this is the textarea I want to use
                    <s:text><![CDATA[
                        Name:
                        Surname:
                        Nickname:
                        Hair Color:

等等。。。

如有任何帮助,请

为什么在这种情况下需要绑定?

您的XML节点已经创建,text属性在同一个函数中进行求值,因此它应该不会成为问题。换句话说,当调用事件处理程序时,text的当前值将被复制到XML节点中,但任何未来的更改都不会反映出来(您可能根本不需要它,因为您在单击"保存"时保存了XML文件)。

至于大括号之间的链,{components.CreateCharacters.mainchar.text}似乎不正确,因为components.CreateCharacters是类引用,而不是直接对组件的引用。相反,您应该使用creatchr

我不确定XML中的变量替换是否支持.。您是否尝试过将text值保存在类似这样的局部变量或类变量中?

    var currentText:String = creatchr.mainchar.text;
    <TXT>{currentText}</TXT>

最新更新