【问题标题】:how can I access a Key value from ResourceDictionary in wpf using mvvm如何使用 mvvm 从 wpf 中的 ResourceDictionary 访问键值
【发布时间】:2012-09-26 10:32:58
【问题描述】:

我在我的 WPF-MVVM 应用程序的 Resource 文件夹下创建了 Resource.fr-CA.xaml、Resource.en-US.xaml 文件。

我创建了 Resource.fr-CA.xaml 和 Resource.en-US.xaml 文件,如下所示:

Resource.fr-CA.xaml

<system:String x:Key="EntUser_PhoneNo_Label">Num\u00e9ro de t\u00e9l\u00e9phone</system:String>   

Resource.en-US.xaml

<system:String x:Key="EntUser_PhoneNo_Label">Phone Number</system:String>    

我的应用程序包含一个组合框来选择不同类型的语言。

如果用户选择 法语,那么我必须从 Reource.fr-CA 或用户获取 "EntUser_PhoneNo_Label" 键值的值选择 English 语言,然后我必须从 Resource.en-US.xaml 中获取价值。

请告诉我如何从后面的代码中从对应的 .xaml 文件的 ResourceDictionary 中获取键/值的解决方案。

【问题讨论】:

  • 大家好,我可以按如下方式访问键值:ResourceDictionary MyResDict = new ResourceDictionary(); MyResDict.Source = new Uri("/MyLang;component/Resources/Resource.fr-CA.xaml", UriKind.RelativeOrAbsolute); byte[] utf8String = Encoding.UTF8.GetBytes(Convert.ToString(MyResDict["LoginButton_Label"]));字符串 str1 = Encoding.UTF8.GetString(utf8String); EntUser_PhoneNo_Label = str1;但在这里我的问题是我无法将 "Num\u00e9ro de t\u00e9l\u00e9phone" 转换为 "Numéro de téléphone"

标签: wpf mvvm


【解决方案1】:

这段代码运行良好:

   byte[] utf8String = Encoding.UTF8.GetBytes("Num\u00e9ro de t\u00e9l\u00e9phone"); 
   string str1 = Encoding.UTF8.GetString(utf8String);

还有这个:

public static class  StringDecoder
    {
        public static string Decode(string str)
        {
            if (str == null)
            {
                return null;
            }
            return HttpUtility.UrlDecode(str, Encoding.UTF8);
        }

    }
var str = StringDecoder.Decode("Num\u00e9ro de t\u00e9l\u00e9phone"); // returns Numéro de téléphone

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多