• silverlight 国际化的一种实现方法


    大致思路如下:定义一个语言转换器并实现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(
    thisnew PropertyChangedEventArgs("AString"));
            }
        public static string AString { get { return "AString"; } }

            
    #endregion
        }
  • 相关阅读:
    练习 字符串存入字典 数组的降序 倒序 字符串目录存不存在 数组中文排序
    随机 随机获得100个50-100的数字字符串,存到数组并输出
    练习 字符串10题
    把一个字符串反输出abc123.xyz789输出987zyx.321cba
    把NSString *string=@"2013 年 05 月 05 日";以2013-05-05输出
    dephi cef3获取Post数据
    Delphi下 多显示器,将窗体显示于第二个显示器
    Delphi下POS机
    SQLConnection 连接 SQLite
    cxGrid主视图取得从视图
  • 原文地址:https://www.cnblogs.com/visi/p/1766599.html
Copyright © 2020-2023  润新知