如何从C#中的资源字典(XAML)获取值 [英] How to get the value from a resource dictionary (XAML) in C#

查看:521
本文介绍了如何从C#中的资源字典(XAML)获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个WPF应用程序,用户可以在其中运行时更改语言(不是当前的区域性!). 因此,我有多个XAML类型的资源字典,在其中添加了一些文本,以使我的WPF应用程序具有多种语言,例如:

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:

<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进行验证. 我的第一个想法是在视图模型中进行验证时将文本输入到键"insert_first_name". 所以我试图通过使用它来获得它:

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:

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

但是当我使用FindName方法时,我得到的是空值.

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

当我尝试

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

我得到"true",表示密钥存在.

i get "true", which means the key exists.

我该如何获取密钥的值?

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

推荐答案

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

To look up app-wide resources from code, use Application.Current.Resources to get the app's resource dictionary, as shown here:

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

这篇关于如何从C#中的资源字典(XAML)获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆