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

标签 c# wpf xaml dictionary

我正在开发一个 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>

WPF 窗口和控件受窗口资源的约束。 但我正在使用 DataAnnotations 进行验证。 我的第一个想法是在我的 View 模型中验证时将文本获取到键“insert_first_name”。 所以我试着用这个来获得它:

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

但是当我使用 FindName 方法时,我得到 null。

当我尝试

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

我得到“真”,这意味着 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");
}

最佳答案

要从代码中查找应用范围的资源,请使用 Application.Current.Resources 获取应用的资源字典,如下所示:

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

Source

关于c# - 如何从 C# 中的资源字典 (XAML) 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41981799/

相关文章:

c# - 如何使用 WPF 创建平面组合框?

c# - 删除WPF JpegBitmapDecoder中的图像

image - 如何将图像背景设置为重复?

c# - 如何在 C# 中使用 LINQ 搜索特定字符串属性的对象列表

c# - 在 IIS-10 中发布后,ASP.NET Core MVC 2.1.0 session 不工作

c# - WPF 按钮背景问题

c# - 如何将 Random().Next() 修改为自增

c# - 元组的替代方法(setter)

c# - 文本 block 中文本部分的多种格式

c# - 我的 Windows 应用商店应用程序在关闭后仍在 Debug模式下运行