开发者

Access main function from component

开发者 https://www.devze.com 2023-01-29 13:25 出处:网络
How do I get \"close\" button to run runFunction() from within component to outside declaration? FxGlobals.topLevelApplication doesn\'t seem to be working for this.

How do I get "close" button to run runFunction() from within component to outside declaration? FxGlobals.topLevelApplication doesn't seem to be working for this.

<fx:Declarations>
        <fx:Component className="MyNativeWindow">
            <s:Window>
                <s:BorderContainer width="100%" height="100%">
                <s:backgroundFill>
                <s:LinearGradient>
                    <s:GradientEntry color="0x555555"/>
                    <s:GradientEntry color="0x777777"/>
                    </s:LinearGradient>
                </s:backgroundFill>
                <mx:Button y="263" x="420" label="close" click="runFunction();this.close()"/>   
                </s:BorderContainer>
            </s开发者_如何学Python:Window>
        </fx:Component>
    </fx:Declarations>


First off, you shouldn't be doing this, more on that in a moment...

If you must do this, (say you have to for some business reason)

You should be able to access the top level application like this...

<fx:Declarations>
    <fx:Component className="MyNativeWindow">
        <s:Window>
            <s:BorderContainer width="100%" height="100%">
            <s:backgroundFill>
            <s:LinearGradient>
                <s:GradientEntry color="0x555555"/>
                <s:GradientEntry color="0x777777"/>
                </s:LinearGradient>
            </s:backgroundFill>
            <mx:Button y="263" x="420" label="close" click="(FlexGlobals.topLevelApplication as MyApplicationType).runFunction();this.close()"/>   
            </s:BorderContainer>
        </s:Window>
    </fx:Component>
</fx:Declarations>

Replacing MyApplicationType with the type of your application.

Now, further to my, do not do this note, above. Your component shouldn't be calling dependent functions this way, instead you should be handling this button click event using a handler function, which then triggers a component level event which is handled at each level up the component tree, all the way to the Application, if that is where this event should be handled.

However, just telling you to do this without knowing the rest of your application structure won't help much, chances are you will need to create the mess before you understand why it should be cleaned.

But as a helpful tip, always try to modularize your application, by separating functional responsibilities into appropriate classes.

0

精彩评论

暂无评论...
验证码 换一张
取 消