c# - 使用 ResourceWrapper 在 Silverlight 4 中进行本地化

标签 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();
}

这适用于 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/

相关文章:

c# - 在 Asp.Net MVC 3 中通过文化和 UI 文化解决字段本地化问题

localization - iReport:如何根据法语区域设置格式化日期

c# - Visual Studio 警告级别的含义?

c# - 如何使用 Selenium ExecuteScript 函数获取 HashTable

c# - 加载虚拟属性

silverlight - Silverlight 应用程序中的文化

c# - 最小占用空间/准系统 ASP.NET Core WebAPI

Silverlight MVVM MEF ViewInjection

silverlight - 从 win 7 手机中的用户控件导航

ruby-on-rails - number_to_currency 语言环境转换