---
title: "FLEX 国际化：刷新DataProvider中的值"
author: "f910230"
date: "2010-07-09T09:26:02Z"
tags: ["Flash", "Flex", "Google"]
categories: ["Web前端"]
slug: "flex-dataprovider"
draft: false
lastmod: "2010-07-09T09:29:43Z"
id: 708861
url: "https://f910230.iteye.com/blog/708861"
---

在FLEX国际化中遇到一个问题，对于DataProvider中的值不能随着改变localeChain的值而动态改变，google了找到以下两篇文章算是解决了问题

[http://stackoverflow.com/questions/354811/flex-localization-refresh-dataprovider-values](http://stackoverflow.com/questions/354811/flex-localization-refresh-dataprovider-values)

[http://www.puxiao.com/?p=201](http://www.puxiao.com/?p=201)

 

大体思路是在改变localeChain 值时发布自定义事件，通过监听这个事件重新刷新DataProvider中的值，但是通过普通的dispatch方法不能全局监听到这个事件，这样通过第二篇文章中的方法设置静态

```
<mx:ToggleButtonBar dataProvider="{dataProviderToggleB} itemClick="clickHandler(event);" selectedIndex="0"> 
```

 的EventDispatcher，就可以在FLEX应用中的任何地方都能监听到这个事件了。

 

\---------------------------------------------------------------------------

Maybe if you make a getter bindable to a custom event for ex: "langChange"

```
[Bindable("langChange")] 
public function get dataProviderToggleB():ArrayCollection 
{ 
    var arr :ArrayCollection = new ArrayCollection(); 
 
    arr.addItem(resourceManager.getString('dashboard','daily')); 
    arr.addItem(resourceManager.getString('dashboard','monthly')); 
 
    return arr;  
} 
```

 

and in your "resourceManager.localeChain" setter you dispatch:

```
dispatchEvent(new Event("langChange")); 
```

 

and you can used like this:

 

\---------------------------------------------------------------------------

自定义CustomEvent类，继承Event，并且给他增加一个dispatcher:EventDispatcher属性。  
使用这个类，就可以抛出事件并附带数据。  
因为使用了 static 设置为静态属性，所以其他任意地方只要注册有这个事件侦听，就能捕获到。

```
CustomEvent:
package
{
import flash.events.Event;
import flash.events.EventDispatcher;

public class CustomEvent extends Event
{
public static const EVENT_NAME:String=”event_name”;

public static const dispatcher:EventDispatcher=new EventDispatcher();
public var data:Object;

public function CustomEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false, data:Object=null)
{
super(type, bubbles, cancelable);
this.data=data;
}
override public function clone():Event{
return new CustomEvent(type, bubbles, cancelable, data);
}
}
}

```

 

抛出事件时：

```
var custom:CustomEvent=new CustomEvent(CustomEvent.CLICK);
//custom.data is Object;//发送事件同时，可以传值(可传可不传)
CustomEvent.dispatcher.dispatchEvent(custom);
```

 

侦听事件时：

```
CustomEvent.dispatcher.addEventListener(CustomEvent.CLICK,clickHandler);
```