我在我的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类,但这使得在2个不同的键中具有相同值的可能性很小,但代码应该对这些键执行不同的操作。 / p>
所以我的问题是,是否有可能让Key像价值一样简单 (伪代码)
Properties.ResourceFile.Keys.ResourceKey
或
Properties.ResourceFile.ResourceKey.Name
答案 0 :(得分:1)
您可以使用所需的所有键创建单独的类
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) {
...
}