您的位置:首页 > 其它

SL - 整理 - 给组件绑定资源文件值

2013-12-11 16:09 134 查看
<TextBlock Height="19" HorizontalAlignment="Left" Margin="280,0,0,0" Text="{Binding Source={StaticResource IssuerStringLibrary}, Converter={StaticResource Localizer}, ConverterParameter=properties_lbl_description}" TextWrapping="Wrap" VerticalAlignment="Top" />

    public class Localizer : System.Windows.Data.IValueConverter
    {

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                var reader = (IStringLibrary)value;
                string resourceValue;
                string resourceName = parameter as string;
               
                if (!string.IsNullOrEmpty(resourceName))
                {
                    resourceValue = reader.Get(resourceName);
                }
                else
                {
                    resourceValue = "ConverterParameter missing in Binding tag";   //ERROR
                }

                return resourceValue;
            }
            catch
            {
                return "Localization Configuration Error";
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        public static string Get(object stringLibrary, string resourceName)
        {
            try
            {
                string resourceValue;
                var reader = (IStringLibrary)stringLibrary;

                if (!string.IsNullOrEmpty(resourceName))
                {
                    resourceValue = reader.Get(resourceName);
                }
                else
                {
                    resourceValue = "Invalid resourceName";   //ERROR
                }

                return resourceValue;
            }
            catch
            {
                return "Localization Configuration Error";
            }
        }

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