您的位置:首页 > 其它

silverlight 国际化的一种实现方法

2010-06-28 11:51 344 查看
大致思路如下:定义一个语言转换器并实现INotifyPropertyChanged接口,在转换器实现的时候调用资源文件,通过PropertyChangedEventHandler通知UI从而实现多语言。

代码

public class LanguageConverter : IValueConverter, INotifyPropertyChanged
{
#region IValueConverter
/// Converter to go find a string based on the UI culture
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
if ((value == null) || !(value is string))
return "set Binding Path/Source!";

return StringResourceManager.GetString(
(string)parameter, System.Threading.Thread.CurrentThread.CurrentUICulture);
}

public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
throw new NotImplementedException("No reason to do this.");
}

#endregion IValueConverter

#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

/// <summary>
/// Change the culture for the application.
/// </summary>
/// <param name="culture">Full culture name</param>
public void ChangeCulture(string culture)
{
System.Threading.Thread.CurrentThread.CurrentUICulture =
new System.Globalization.CultureInfo(culture);
System.Threading.Thread.CurrentThread.CurrentCulture =
System.Threading.Thread.CurrentThread.CurrentUICulture;
DefaultResource.Culture = System.Threading.Thread.CurrentThread.CurrentUICulture;

// notify that the culture has changed
PropertyChangedEventHandler handler = PropertyChanged;

if (handler != null)
handler(this, new PropertyChangedEventArgs("AString"));
}
    public static string AString { get { return "AString"; } }

#endregion
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: