获取ResourceKey,而不是转换后的值
本文关键字:转换 ResourceKey 获取 | 更新日期: 2023-09-27 18:03:00
我在我的WPF应用程序中使用这个库进行本地化:http://wpflocalizeextension.codeplex.com/
它有一个功能,从我的xaml定义中获得绑定的资源键,例如:
XAML:<Button Content="{lex:loc Assembly:ResourceFile:ResourceKey}" Click="Button_Click" />
<Button Content="{lex:loc Assembly:ResourceFile:ResourceKey111}" Click="Button_Click" />
<Button Content="{lex:loc Assembly:ResourceFile:ResourceKey112}" Click="Button_Click" />
<Button Content="{lex:loc Assembly:ResourceFile:ResourceKey113}" Click="Button_Click" />
<Button Content="{lex:loc Assembly:ResourceFile:ResourceKey114}" Click="Button_Click" />
<Button Content="{lex:loc Assembly:ResourceFile:ResourceKey115}" Click="Button_Click" />
背后的代码:
private void Button_Click (object sender, RoutedEventArgs e)
{
var bound = LocExtension.GetBoundExtension(sender, ContentPresenter.ContentProperty.Name);
if (bound == null)
return;
var key = bound.Key;
var resourceIdentifierKey = bound.ResourceIdentifierKey;
}
你可以看到我可以得到Key
和ResourceIdentifierKey
,现在我想能够比较这个绑定键与给定键:
private void Button_Click (object sender, RoutedEventArgs e)
{
var bound = LocExtension.GetBoundExtension(sender, ContentPresenter.ContentProperty.Name);
if (bound == null)
return;
var key = bound.Key;
var resourceIdentifierKey = bound.ResourceIdentifierKey;
if (key == "Assembly:ResourceFile:ResourceKey")
//do something...
if (key == "Assembly:ResourceFile:ResourceKey111")
//do something else...
if (key == "Assembly:ResourceFile:ResourceKey112")
//do something else...
if (key == "Assembly:ResourceFile:ResourceKey113")
//do something else...
if (key == "Assembly:ResourceFile:ResourceKey114")
//do something else...
if (key == "Assembly:ResourceFile:ResourceKey115")
//do something else...
}
这个实现会产生魔法字符串,并且在重命名时可能无法获得全部字符串。我还可以像这样比较解析后的值:
private void Button_Click (object sender, RoutedEventArgs e)
{
var bound = LocExtension.GetBoundExtension(sender, ContentPresenter.ContentProperty.Name);
if (bound == null)
return;
var key = bound.Key;
var resourceIdentifierKey = bound.ResourceIdentifierKey;
string value = null;
key.ResolveLocalizedValue<string>(out value);
if (value == Properties.ResourceFile.ResourceKey)
//do something...
if (value == Properties.ResourceFile.ResourceKey111)
//do something else...
if (value == Properties.ResourceFile.ResourceKey112)
//do something else...
if (value == Properties.ResourceFile.ResourceKey113)
//do something else...
if (value == Properties.ResourceFile.ResourceKey114)
//do something else...
if (value == Properties.ResourceFile.ResourceKey115)
//do something else...
}
这可以防止魔术字符串,因为它使用了从应用程序中的资源处理自动生成的Properties类,但这给了在两个不同的键中具有相同值的小机会,但代码应该在这些键上执行不同。
所以我的问题是,是否有可能得到键像值一样容易(伪代码)
Properties.ResourceFile.Keys.ResourceKey
或
Properties.ResourceFile.ResourceKey.Name
你可以用你需要的所有键创建一个单独的类
public class MyResourceKeys {
public static string ResourceKey { get { return "ResourceKey"; } }
public static string ResourceKey111 { get { return "ResourceKey111"; } }
public static string ResourceKey112 { get { return "ResourceKey112"; } }
...
}
,然后使用
if (key == MyResourceKeys.ResourceKey) {
...
}