[英]What is the proper way to bind a dynamic value from a language file?
I have a view model which has the name of an entry in my language resource file. 我有一个视图模型,在我的语言资源文件中有一个条目的名称。
I tried to bind this value directly to the x:Uid attribute of the TextBlock
in my XAML, but got a XAML error. 我试图将此值直接绑定到我的XAML中的
TextBlock
的x:Uid属性,但是出现了XAML错误。
To get around this limitation I though about changing the property to return the value from the language file, but was concerned that doing so might not be a valid MVVM solution. 为了解决这个限制,我想改变属性以从语言文件中返回值,但是担心这样做可能不是有效的MVVM解决方案。
I also thought about creating a converter to set the text. 我还想过创建一个转换器来设置文本。
Way that does not work: 不起作用的方式:
<StackPanel Orientation="Horizontal">
<Image Margin="0,0,20,0" Source="{Binding IconPath}" />
<TextBlock x:Uid="{Binding LanguageResourceName}" />
</StackPanel>
The view model I am binding to: 我绑定的视图模型:
class Tab : ViewModelBase
{
private string _IconPath,
_LanguageResourceName;
private ViewModelBase _ViewModel;
/// <summary>
/// The path to the icon to show on the tab.
/// </summary>
public string IconPath
{
get { return _IconPath; }
set { SetProperty(ref _IconPath, value); }
}
/// <summary>
/// The name of the entry in the language resource file to display on the tab.
/// </summary>
public string LanguageResourceName
{
get { return _LanguageResourceName; }
set { SetProperty(ref _LanguageResourceName, value); }
}
/// <summary>
/// The contents of the tab.
/// </summary>
public ViewModelBase ViewModel
{
get { return _ViewModel; }
set { SetProperty(ref _ViewModel, value); }
}
}
So what is the correct way to resolve this issue? 那么解决这个问题的正确方法是什么?
Converter is the best way to do it. 转换器是最好的方法。 Check out my answer here for a quick explanation.
在这里查看我的答案,以便快速解释。 I've copied the Converter I define there below.
我已经复制了我在下面定义的转换器。
ResourceController
is a simple Controller which gets a reference to a ResourceLoader
and retrieves values via a method GetString(string resourceId)
. ResourceController
是一个简单的Controller,它获取对ResourceLoader
的引用,并通过方法GetString(string resourceId)
检索值。
public class ResourceTranslationConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var valString = value as string;
// If what is being converted is a string, return the resource translation
// Else return something else, such as the object itself
return valString == null ? value : ResourceController.GetString(valString);
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
Binding then works like: 绑定然后工作如下:
<TextBlock Text="{Binding LanguageResourceName, Converter={StaticResource ResourceTranslationConverter}}" />
Make sure you've defined an accessible ResourceTranslationConverter
. 确保您已定义可访问的
ResourceTranslationConverter
。 Possibly in the Page.Resources
or even in your App.xaml
(since you should only need one static reference). 可能在
Page.Resources
,甚至在App.xaml
(因为您只需要一个静态引用)。
Hope this helps and happy coding! 希望这有助于编码!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.