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
}
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
}
相关文章推荐
- SL - 整理 - 定义Dependency属性(使自定义组件属性能被绑定)
- Github开源Android组件资源整理(一) 个性化控件(View)
- 整理Silverlight资源列表(三)-SL实际运用案例
- Github开源Android组件资源整理(二)ActionBar和Menu
- Github开源Android组件资源整理(三) ViewPager 、Gallery、GridView
- Github开源Android组件资源整理(四) ImageView, ProgressBar,TextView
- android 开发工具,资源,组件,风格整理集合
- Github开源Android组件资源整理(五)ScrollView, TimeView, TipView, FlipView, ColorPickView
- SL - 整理 - 将验证做到资源文件中
- Github开源Android组件资源整理(一) 个性化控件(View)
- Github开源Android组件资源整理
- Github开源Android组件资源整理(二)ActionBar和Menu
- Github开源Android组件资源整理(三) ViewPager 、Gallery、GridView
- 【VUE】.NET实现Tree组件双向绑定数据(3)-资源分配
- Github开源Android组件资源整理(四) ImageView, ProgressBar,TextView
- github开源Android组件资源整理(五)ScrollView, TimeView, TipView, FlipView, ColorPickView
- github开源Android组件资源整理(六)GraphView, UI Style
- 整理Silverlight资源列表(三)-SL实际运用案例
- Github开源Android组件资源整理(七)其他组件
- 整理过的资源入口(大部分可用)