如何设置语言环境类和 UI 组件之间的绑定

发布于 2024-12-08 14:15:32 字数 872 浏览 0 评论 0原文

我试图弄清楚如何正确设置 Locale 类和 mxml 文件之间的绑定。

我的主 mxml 文件包含:

<mx:Button id="TMP" label="{Locale.getLocaleString('title'}"/>

我想在按钮的标签中显示一个空字符串,直到加载区域设置文件。加载语言环境文件后,我想显示 getLocaleString('title') 返回的内容。

我认为这就是绑定的目的,但我认为我的做法是错误的。

我知道我不允许绑定函数。那么我是否需要允许访问(使用 get 函数)我的 _dictionary 属性?为这样的事情设置绑定的标准方法是什么?

这是我当前的 Locale 类。

public class Locale {

    private static var _dictionary:Dictionary = new Dictionary();

    public static function loadResources():void {...}

    public static function getLocaleString(featureID:String):String {
        if(_dictionary[featureID]==null){
            return "";
        }
        return _dictionary[featureID];
    }
}

I am trying to figure out how to properly setup binding between my Locale class and an mxml file.

My main mxml file contains:

<mx:Button id="TMP" label="{Locale.getLocaleString('title'}"/>

I would like to display an empty string in the button's label until the locale file loads. Once the locale file loads I would like to display what is returned by getLocaleString('title').

I think that this is what Binding is intended for however I think that I am approaching it wrong.

I know that I am not allowed to bind on a function. So do I need to allow access (using a get function) to my _dictionary property? What is the standard way to setup Binding for something like this?

This is my current Locale class.

public class Locale {

    private static var _dictionary:Dictionary = new Dictionary();

    public static function loadResources():void {...}

    public static function getLocaleString(featureID:String):String {
        if(_dictionary[featureID]==null){
            return "";
        }
        return _dictionary[featureID];
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不是你的备胎 2024-12-15 14:15:32

这是我找到的解决方案:

  • 扩展 EventDispatcher,使类可绑定并在加载完成时调度一个事件。 (doc)
  • 将类从单例的静态类(doc1doc2

这是我的最终代码:

[Bindable(event="loaded")]
public class LocaleSingleton extends EventDispatcher{

    private var _dictionary:Dictionary = new Dictionary();

    public function LocaleSingleton(){/* Do check to make sure it is a singleton*/}

    public function get instance(){...}

    public function loadResources():void {
    ...
    dispatchEvent(new Event("loaded")); // Method in EventDispatcher
    }

    public function getLocaleString(featureID:String):String {
    if(_dictionary[featureID]==null){
        return "";
    }
    return _dictionary[featureID];
    }
}

This is the solution I found:

  • Extend the EventDispatcher, make the class Bindable and dispatch an event when loading has completed. (doc)
  • Turn the class from a static class to a singleton (doc1, doc2)

Here is my final code:

[Bindable(event="loaded")]
public class LocaleSingleton extends EventDispatcher{

    private var _dictionary:Dictionary = new Dictionary();

    public function LocaleSingleton(){/* Do check to make sure it is a singleton*/}

    public function get instance(){...}

    public function loadResources():void {
    ...
    dispatchEvent(new Event("loaded")); // Method in EventDispatcher
    }

    public function getLocaleString(featureID:String):String {
    if(_dictionary[featureID]==null){
        return "";
    }
    return _dictionary[featureID];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文