Tal*_*ang 5 c# wpf xaml dynamicresource caliburn
我目前正在开发一个项目,我使用Caliburn在View和ViewModel之间进行绑定.为了能够在运行时在语言之间切换,我有单独的资源文件,其中包含应用程序中使用的所有字符串.一些例如TextBlock文本绑定绑定到其中一个字符串资源,如下所示:
SampleView.xaml
<TextBlock Text={DynamicResource Foo.Bar.Baz} ... />
Language.en-US.xaml
<system:String x:Key="Foo.Bar.Baz">Im a string</system:String>
当我将应用程序的文化更改为其他语言时,对Foo.Bar.Baz 的动态绑定使字符串在运行时更新为新语言.大!
但是,应用程序中的某些Text属性绑定到带有Caliburn的ViewModel中的字符串,如下所示:
SampleView.xaml
<TextBlock Text={Binding SampleText} ... />
SampleViewModel.cs
public string SampleText { get; set; }
值的值SampleText设置为Language.en-US.xaml中的字符串资源,如下所示:
...
SampleText = Application.Current.FindResource("Foo.Bar.Baz") as string;
...
不幸的是,当我更改应用程序文化时,字符串SampleText不会更新.
问题是: 如何将SampleText设置为Language.en-US.xaml中的字符串资源,当我更改应用程序文化时,它将自动更新?
注意:通过对此StackOverflow问题的评论,我读到可以通过类似的bindnig:
SampleText = Application.Current.Resource["Foo.Bar.Baz"] as string;
但是,这对我不起作用.
您可以将您的资源作为 DynamicResource 来引用。要在后面的代码中执行此操作:
frameworkElement.SetResourceReference(dependencyProperty, resourceKey);
Run Code Online (Sandbox Code Playgroud)
例如:
mySampleTextBlock.SetResourceReference(TextProperty, "Foo.Bar.Baz")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3110 次 |
| 最近记录: |