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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41981799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:59:56  来源:igfitidea点击:

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

c#wpfxamldictionary

提问by Patrick Pirzer

I'm working on a WPF-application in which the user can change the language (not the current culture!) at runtime. So i have multiple resource dictionaries of type XAML to which i added texts for making my WPF-app multilingual like that:

我正在开发一个 WPF 应用程序,用户可以在其中在运行时更改语言(不是当前的文化!)。所以我有多个 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. But i'm using DataAnnotations for validation. My first thought was to get the text to the key "insert_first_name" while the validation in my viewmodel. So i tried to get it by using this:

WPF 窗口和控件由窗口资源绑定。但我使用 DataAnnotations 进行验证。我的第一个想法是在我的视图模型中进行验证时将文本获取到键“insert_first_name”。所以我试图通过使用它来获取它:

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

But when i use the method FindName, i get null.

但是当我使用 FindName 方法时,我得到了空值。

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

回答by parameter

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

来源