Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用ResourceWrapper在Silverlight 4中进行本地化_C#_Silverlight_Localization - Fatal编程技术网

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

C# 使用ResourceWrapper在Silverlight 4中进行本地化,c#,silverlight,localization,C#,Silverlight,Localization,我有一个业务应用程序(从模板创建),我可以通过使ResourceWrapper InotifyProperty更改,然后添加代码来动态更改语言: private void Language_SelectionChanged(object sender, SelectionChangedEventArgs e) { Thread.CurrentThread.CurrentCulture = new CultureInfo(((ComboBoxItem)((ComboBox)sender

我有一个业务应用程序(从模板创建),我可以通过使ResourceWrapper InotifyProperty更改,然后添加代码来动态更改语言:

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文件(即主页框架)中引用/绑定的资源上运行良好,但它不会更新我在代码中声明的任何引用,即

InfoLabel.Content = ApplicationStrings.SomeString
目前我没有使用ResourceWrapper。我这里的问题是如何更改代码,使其在ResourceWrapper更改时使用并更新。我试过:

InfoLabel.Content = ((ResourceWrapper)App.Current.Resources["ResourceWrapper"])
    .ApplicationStrings.SomeString
但它不起作用


有什么想法吗?

您必须在代码中创建一个绑定。大概是这样的:

var b = new Binding("SomeString");
b.Source = ((ResourceWrapper)App.Current.Resources["ResourceWrapper"]).ApplicationStrings;
b.Mode = BindingMode.OneWay;
InfoLabel.SetBinding(ContentControl.ContentProperty, b);
InfoLabel.SetBinding(ContentControl.ContentProperty, GetResourceBinding("SomeString"));
请记住,绑定到的类必须实现
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;
        }
然后使用以下帮助器方法:

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

对于这样一件简单的事情来说,这似乎是一项艰巨的工作。如果我必须在代码中绑定每个字符串,我可能会使用NavigationService.Refresh(),但这在视觉上并不优雅……你是对的,它有很多代码。这就是为什么最好避免它并直接在XAML中声明绑定。您必须在代码中设置contentcontrol的文本有什么原因吗?我添加了一个帮助器方法,将代码量减少到一行。I+1并标记为已应答,但我仍然必须使用刷新,因为DataGrid列标题不喜欢绑定,并且对于某些标签,我使用“some String”+:“我不知道绑定时如何连接字符串。谢谢你的帮助。好的,我会给你一些建议。对于datagrid列标题,可以避免直接设置Header属性,而是使用绑定到ResourceWrapper的TextBlock或ContentControl。至于格式化,您可以通过在绑定上使用值转换器来解决它。Tim Heuer在这里有一篇关于值转换器的文章: