大致思路如下:定义一个语言转换器并实现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
    }

 

 

posted on 2010-06-28 11:51  visi  阅读(459)  评论(0编辑  收藏  举报