简体   繁体   English

如何从C#中的资源字典(XAML)获取值

[英]How to get the value from a resource dictionary (XAML) in C#

I'm working on a WPF-application in which the user can change the language (not the current culture!) at runtime. 我正在开发一个WPF应用程序,用户可以在其中运行时更改语言(不是当前的区域性!)。 So i have multiple resource dictionaries of type XAML to which i added texts for making my WPF-app multilingual like that: 因此,我有多个XAML类型的资源字典,在其中添加了一些文本,以使WPF应用程序具有多种语言,例如:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:system="clr-namespace:System;assembly=mscorlib"
                    xmlns:local="clr-namespace:Validation_DataAnnotations2.Resources">
    <system:String x:Key="firstname">First name</system:String>
    <system:String x:Key="lastname">Last name</system:String>
    <system:String x:Key="mainwindowtitle">Validation with DataAnnotations</system:String>
    <system:String x:Key="german_language">German</system:String>
    <system:String x:Key="english_language">English</system:String>
    <system:String x:Key="insert_first_name">The first name has to be inserted</system:String>
</ResourceDictionary>

The WPF-windows and controls are bound by the window resources. WPF窗口和控件受窗口资源的约束。 But i'm using DataAnnotations for validation. 但是我正在使用DataAnnotations进行验证。 My first thought was to get the text to the key "insert_first_name" while the validation in my viewmodel. 我的第一个想法是在视图模型中进行验证时将文本输入键“ insert_first_name”。 So i tried to get it by using this: 所以我试图通过使用它来获得它:

System.Windows.Application.Current.Resources.FindName("insert_first_name")

But when i use the method FindName, i get null. 但是,当我使用FindName方法时,我得到了null。

And when i try 当我尝试

System.Windows.Application.Current.Resources.Contains("insert_first_name")

i get "true", which means the key exists. 我得到“ true”,这意味着密钥存在。

How can i get the value to key? 我怎样才能得到关键的价值?

protected void ValidateModel()
{
    validationErrors.Clear();
    ICollection<ValidationResult> validationResults = new List<ValidationResult>();
    ValidationContext validationContext = new ValidationContext(personmodel, null, null);
    if (!Validator.TryValidateObject(personmodel, validationContext, validationResults, true))
    {
        foreach (ValidationResult validationResult in validationResults)
        {
            string property = validationResult.MemberNames.ElementAt(0);
            if (validationErrors.ContainsKey(property))
            {
                validationErrors[property].Add(validationResult.ErrorMessage);
            }
            else
            {
                validationErrors.Add(property, new List<string> { validationResult.ErrorMessage });
                if (validationResult.ErrorMessage == "insert_first_name")
                {
                    var text = System.Windows.Application.Current.Resources.FindName("insert_first_name");
                }
            }
        }
    }

    // Raises the ErrorsChanged for all properties explicitly.
    RaiseErrorsChanged("FirstName");
    RaiseErrorsChanged("LastName");
}

To look up app-wide resources from code, use Application.Current.Resources to get the app's resource dictionary, as shown here: 要从代码中查找应用程序范围内的资源,请使用Application.Current.Resources获取应用程序的资源字典,如下所示:

string insertFirstName = Application.Current.Resources["insert_first_name"];

Source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM