Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取ResourceKey而不是转换后的值_C#_Wpf_Xaml_Localization - Fatal编程技术网

C# 获取ResourceKey而不是转换后的值

C# 获取ResourceKey而不是转换后的值,c#,wpf,xaml,localization,C#,Wpf,Xaml,Localization,我正在WPF应用程序中使用此库进行本地化: 它具有从我的xaml定义中获取绑定资源密钥的功能,例如: private void Button_Click (object sender, RoutedEventArgs e) { var bound = LocExtension.GetBoundExtension(sender, ContentPresenter.ContentProperty.Name); if (bound == null) return;

我正在WPF应用程序中使用此库进行本地化:

它具有从我的xaml定义中获取绑定资源密钥的功能,例如:

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

如您所见,我可以获得
以及
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...
}


您可以使用所需的所有键创建单独的类

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

如果你能解释一下否决票,我就可以改进这个问题了。大多数(或所有)C#类都自动有一个
.ToString()
方法,所以这可能是一种可能性。另外,
switch()
语句可能比那些
if
s.@hosch250语句工作得更好。您建议我调用.ToString(),该变量包含您需要的字符串。我对这些属性一无所知,但我知道C#在其所有(或大部分)函数上都有一个
.ToString()
。我认为它类似于
if(等于(bound.Key.ToString(),“/*string here*/”)
您可以做的另一件事是创建6个不同版本的
按钮\u单击
。这就是我要做的。
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) {
    ...
}