Localization in Silverlight 4 using ResourceWrapper

I have a business application (created from a template), and I can dynamically change the language by creating ResourceWrapper INotifyPropertyChanged and then adding to the code:

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

this works fine with the resources referenced / bound in xaml files (i.e. the MainPage frame), but does not update the links to everything that I declared in the ie code

InfoLabel.Content = ApplicationStrings.SomeString

I am not currently using ResourceWrapper. My question here is how can I change my code so that it uses it and is updated when ResourceWrapper changes. I tried:

InfoLabel.Content = ((ResourceWrapper)App.Current.Resources["ResourceWrapper"])
    .ApplicationStrings.SomeString

but that will not work.

Any ideas?

+5
source share
1 answer

You will need to create a binding in the code. Something like that:

var b = new Binding("SomeString");
b.Source = ((ResourceWrapper)App.Current.Resources["ResourceWrapper"]).ApplicationStrings;
b.Mode = BindingMode.OneWay;
InfoLabel.SetBinding(ContentControl.ContentProperty, b);

, , , INotifyPropertyChanged.



EDIT: , - :

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"));
+2

All Articles