简体   繁体   English

制作XML构建的组件会唤起非接口代码

[英]Making a XML-built component evoke a non-interface code

I'm making a game using HaxeUI. 我正在使用HaxeUI进行游戏。 I've designed a simple UI using XML definition. 我使用XML定义设计了一个简单的UI。 I need the button to execute a code that is unrelated to the UI elements, however, the parsed code from onClick property does not see any local identifiers defined in the area, in which the interface is being built. 我需要按钮来执行与UI元素无关的代码,但是,来自onClick属性的解析代码没有看到在构建接口的区域中定义的任何本地标识符。

How to work around this without the necessity to refrain from using XML definition? 如何在不必使用XML定义的情况下解决这个问题?

Currently there isnt a method exactly as you describe, in the sense that you define your click handler in XML and that links to haxe code - there is this open issue which is essentially as you describe: https://github.com/haxeui/haxeui-core/issues/196 - and I think it would a useful addition. 目前,没有一种方法与您描述的完全一致,因为您在XML中定义了单击处理程序并链接到haxe代码 - 这个开放性问题基本上与您描述的一样: https//github.com/haxeui/ haxeui-core / issues / 196 - 我认为这将是一个有用的补充。

There is however this method (using build macros to build a custom component): 但是有这种方法(使用构建宏来构建自定义组件):

@:build(haxe.ui.macros.ComponentMacros.build("myxml.xml"))
class MySomething extends Box {
    public function new() {
        super();
        myButton.onClick = function(e) {
            myLabel.text = "Clicked";
        }
    }
}

This macro takes your xml (see below) and creates correctly typed member variables of anything that has an id. 这个宏接受你的xml(见下文),并创建任何具有id的类型的正确类型的成员变量。

<vbox>
    <button id="myButton" />
    <label id="myLabel" />
</vbox>

This can then be used in code or xml freely: 然后可以在代码或xml中自由使用:

Screen.instance.addComponent(new MySomething());
Screen.instance.addComponent(new MySomething());
Screen.instance.addComponent(new MySomething());

or 要么

<vbox>
    <mysomething />
    <mysomething />
    <mysomething />
</vbox>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM