我在WPF应用程序中使用了MVVM模式,并将带有本地化的DisplayAttribute应用于我的模型的属性:
class MyModel
{
[Display(
ResourceType = typeof(Resources.Strings),
Name = "MyPropertyName",
Description = "MyPropertyDescription")]
public double MyProperty { get; set; }
}我想显示模型属性的本地化属性属性。必要的资源文件位于适当的位置,设置了必要的区域性。我还按照DisplayAttribute的要求,将Build Action设置为Embedded Resource,并将Custom Tool设置为PublicResXFileCodeGenerator。但是DisplayAttribute为其Name参数返回"MyPropertyName“,而不是本地化字符串。
我使用DisplayProperty的场景如下所示。为了显示模型属性的属性属性,我使用了一个带有helper类的转换器,如下所示:
namespace MyProject.Converters
{
// helper class
public class MetadataParameters
{
public Type ModelType { get; set; }
public string ModelProperty { get; set; }
public Type AttributeType { get; set; }
public string AttributeProperty { get; set; }
}
public class MetadataConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var mp = parameter as MetadataParameters;
var modelPropertyInfo = mp.ModelType.GetProperty(mp.ModelProperty);
var attribute = modelPropertyInfo
.GetCustomAttributes(true)
.Cast<Attribute>()
.FirstOrDefault(memberInfo => memberInfo.GetType() == mp.AttributeType);
var attributeProperty = attribute.GetType().GetProperty(mp.AttributeProperty);
return attributeProperty.GetValue(attribute, null);
}
}
}XAML资源:
xmlns:converters="clr-namespace:MyProject.Converters"
<converters:MetadataConverter x:Key="metadataConverter" />XAML:
xmlns:converters="clr-namespace:MyProject.Converters"
xmlns:DataAnnotations="clr-namespace:System.ComponentModel.DataAnnotations;assembly=System.ComponentModel.DataAnnotations"
xmlns:Models="clr-namespace:MyProject.Models"
<TextBlock
<TextBlock.Text>
<Binding
Mode="OneWay"
Converter="{StaticResource metadataConverter}">
<Binding.ConverterParameter>
<converters:MetadataParameters
ModelType="{x:Type Models:Model}"
ModelProperty="ModelProperty"
AttributeType="{x:Type DataAnnotations:DisplayAttribute}"
AttributeProperty="Name" />
</Binding.ConverterParameter>
</Binding>
</TextBlock.Text>
</TextBlock>我编写了自己的简单属性,它返回本地化的字符串,而不仅仅是属性名称的值(如DisplayAttribute所做的):
public class LocalizedDisplayAttribute : Attribute
{
private readonly string name;
public virtual string Name
{
get
{
var rm = new System.Resources.ResourceManager(typeof(Resources.Strings));
return rm.GetString(this.name);
}
}
public LocalizedDisplayAttribute(string name)
: base()
{
this.name = name;
}
}
class MyModel
{
[LocalizedDisplay("MyPropertyName")]
public double MyProperty { get; set; }
}那么为什么DisplayAttribute不本地化它的属性呢?MSDN说这个属性是为本地化而设计的。在我的ASP.NET MVC项目中,DisplayAttribute可以正常工作。
发布于 2013-05-09 20:17:48
您需要调用DisplayAttribute上的Get方法和其他一些方法,它们将为您提供您正在查找的本地化字符串。这里要做的是获取DisplayAttribute.Name属性的值,即"MyPropertyName“。
如果调用DisplayAttribute.GetName(),它将查找由ResourceType属性指定的资源类,然后使用Name属性来反映资源类型上的属性。
我更改了您的代码,假设会为您请求的AttributeProperty获取一个GetXXX()方法。
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var mp = parameter as MetadataParameters;
var modelPropertyInfo = mp.ModelType.GetProperty(mp.ModelProperty);
var attribute = modelPropertyInfo
.GetCustomAttributes(true)
.Cast<Attribute>()
.FirstOrDefault(memberInfo => memberInfo.GetType() == mp.AttributeType);
// We have to call the GetXXX methods on the attribute to get a localised result.
//return ((DisplayAttribute)attribute).GetName();
var result = attribute
.GetType()
.InvokeMember(
"Get" + mp.AttributeProperty,
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
null,
attribute,
new object[0]);
return result;
}https://stackoverflow.com/questions/14573757
复制相似问题