Вопрос

I am using this libary for localization in my WPF application: http://wpflocalizeextension.codeplex.com/

It has a feature to get the bound resource key from my xaml definition for example:

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" />

Code Behind:

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;
}

As you can see I can get the Key aswell as ResourceIdentifierKey, now I would like to be able to compare this bound key with a given key:

For example:

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...
}

This implementation result into magic strings and you may not get them all while renaming. I could also compare with the resolved value like this:

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...
}

This prevents magic strings, since its using the Properties class thats autogenerated from the Resource handling in an application, but this gives the small chance for having the same value inside 2 different keys but the code should execute differently upon those keys.

So my question is, is there a possibility to get the Key as easy as the value like (Pseudo code)

Properties.ResourceFile.Keys.ResourceKey 

or

Properties.ResourceFile.ResourceKey.Name
Это было полезно?

Решение

You can create separate class with all keys you need

public class MyResourceKeys {
    public static string ResourceKey { get { return "ResourceKey"; } }
    public static string ResourceKey111 { get { return "ResourceKey111"; } }
    public static string ResourceKey112 { get { return "ResourceKey112"; } }
    ...
}

and then use it

if (key == MyResourceKeys.ResourceKey) {
    ...
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top