我在WPF应用程序中使用MVVM模式,并将DisplayAttribute与本地化应用于我的模型的属性:
class MyModel
{
[Display(
ResourceType = typeof(Resources.Strings),
Name = "MyPropertyName",
Description = "MyPropertyDescription")]
public double MyProperty { get; set; }
}
我想显示model属性的本地化属性。必要的资源文件位于适当的位置,设置必要的文化。我还将Build Action设置为Embedded Resource,将Custom Tool设置为PublicResXFileCodeGenerator - 根据DisplayAttribute的要求。但是DisplayAttribute为其Name参数返回“MyPropertyName”而不是本地化字符串。
我使用DisplayProperty的场景如下所示。为了显示模型属性的属性,我使用带有辅助类的转换器,如下所示:
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正常工作。
答案 0 :(得分:4)
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;
}