我有一个业务应用程序(从模板创建),我可以通过使 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();
}
这适用于 xaml 文件(即 MainPage 框架)中引用/绑定(bind)的资源,但它不会更新我在代码中声明的任何内容的引用,即
InfoLabel.Content = ApplicationStrings.SomeString
目前我没有使用 ResourceWrapper。我的问题是如何更改我的代码以便它在 ResourceWrapper 更改时使用它并更新。我试过:
InfoLabel.Content = ((ResourceWrapper)App.Current.Resources["ResourceWrapper"])
.ApplicationStrings.SomeString
但它不起作用。
有什么想法吗?
最佳答案
您必须在代码中创建绑定(bind)。像这样:
var b = new Binding("SomeString");
b.Source = ((ResourceWrapper)App.Current.Resources["ResourceWrapper"]).ApplicationStrings;
b.Mode = BindingMode.OneWay;
InfoLabel.SetBinding(ContentControl.ContentProperty, b);
请记住,您绑定(bind)的类必须实现 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;
}
然后像这样使用辅助方法:
InfoLabel.SetBinding(ContentControl.ContentProperty, GetResourceBinding("SomeString"));
关于c# - 使用 ResourceWrapper 在 Silverlight 4 中进行本地化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2729488/