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
}
代码
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
}
相关文章推荐
- Silverlight仿Flash换肤一种简单实现方法
- 使用ListResourceBundle类实现国际化的一种方法
- 用javascript结合struts2实现国际化的一种比较戳的方法
- python一种用classmethod实现单例模式方法的探讨
- 一种点对点文件断点续传的多线程实现方法
- 多叉树结合JavaScript树形控件实现无限级树形菜单(一种构建多级有序树形结构JSON(或XML)数据源的方法)
- ArrayList深拷贝的一种实现方法
- android关于实现EditText中加多行下划线的的一种方法
- 实现对gridview删除行时弹出确认对话框的一种简单方法
- 稳扎稳打Silverlight(32) - 2.0Tip/Trick之MessageBox, Popup, 循环的几种实现方法, 动态变换主题, 本地化(多语言), 响应鼠标双击事件
- Java基础:一种实现数据库连接池的方法
- Angular 项目实现国际化的方法
- 一种Windows下线程同步的实现方法 .
- 一种基于im客户端实现消息图片管理的方法和装置
- js函数参数设置默认值的一种变通实现方法
- 一种软阴影的实现方法
- Laravel 5.3 使用内置的 Auth 组件实现多用户认证功能以及登陆才能访问后台的功能的一种实现方法
- 一种M2M业务的架构及实现M2M业务的方法
- 简洁常用权限系统的设计与实现(七):一种错误的不可行的构造树的方法
- 状态机的一种实现方法