Art*_*tur 5 c# silverlight localization
我有一个业务应用程序(从模板创建),我可以通过使ResourceWrapper INotifyPropertyChanged动态更改语言,然后添加代码:
private void Language_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Thread.CurrentThread.CurrentCulture =
new CultureInfo(((ComboBoxItem)((ComboBox)sender).SelectedItem).Tag.ToString());
Thread.CurrentThread.CurrentUICulture =
new CultureInfo(((ComboBoxItem)((ComboBox)sender).SelectedItem).Tag.ToString());
((ResourceWrapper)App.Current.Resources["ResourceWrapper"]).ApplicationStrings =
new ApplicationStrings();
}
Run Code Online (Sandbox Code Playgroud)
这适用于xaml文件中引用/绑定的资源(即MainPage框架),但它不会更新我在代码中声明的任何内容的引用,即
InfoLabel.Content = ApplicationStrings.SomeString
Run Code Online (Sandbox Code Playgroud)
目前我没有使用ResourceWrapper.我的问题是如何更改我的代码,以便在ResourceWrapper更改时使用它并进行更新.我试过了:
InfoLabel.Content = ((ResourceWrapper)App.Current.Resources["ResourceWrapper"])
.ApplicationStrings.SomeString
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
有任何想法吗?
您必须在代码中创建绑定。像这样的东西:
var b = new Binding("SomeString");
b.Source = ((ResourceWrapper)App.Current.Resources["ResourceWrapper"]).ApplicationStrings;
b.Mode = BindingMode.OneWay;
InfoLabel.SetBinding(ContentControl.ContentProperty, b);
Run Code Online (Sandbox Code Playgroud)
请记住,您绑定的类必须实现INotifyPropertyChanged.
编辑:
如果您担心代码量,只需在应用程序中的某个位置创建一个辅助方法:
public Binding GetResourceBinding(string key)
{
var b = new Binding(key);
b.Source = ((ResourceWrapper)App.Current.Resources["ResourceWrapper"]).ApplicationStrings;
b.Mode = BindingMode.OneWay;
return b;
}
Run Code Online (Sandbox Code Playgroud)
然后使用这样的辅助方法:
InfoLabel.SetBinding(ContentControl.ContentProperty, GetResourceBinding("SomeString"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1985 次 |
| 最近记录: |