我正在尝试在我的WPF项目中使用DataAnnotations来指定字符串的最大长度,代码如下:
using System.ComponentModel.DataAnnotations;
然而,我遇到了错误:
类型或命名空间名称'DataAnnotations'在命名空间'System.ComponentModel'中不存在(是否缺少程序集引用?)
我看到其他示例中DataAnnotations
确实存在于这个命名空间中。我正在使用C#4。有什么原因我不能使用它?我该怎么解决?
我正在尝试在我的WPF项目中使用DataAnnotations来指定字符串的最大长度,代码如下:
using System.ComponentModel.DataAnnotations;
然而,我遇到了错误:
类型或命名空间名称'DataAnnotations'在命名空间'System.ComponentModel'中不存在(是否缺少程序集引用?)
我看到其他示例中DataAnnotations
确实存在于这个命名空间中。我正在使用C#4。有什么原因我不能使用它?我该怎么解决?
您需要引用该命名空间所定义的程序集(这在Visual Studio模板中默认情况下不会被引用)。打开引用管理器,添加对System.ComponentModel.DataAnnotations程序集的引用(解决方案资源管理器->添加引用->选择.Net选项卡->从列表中选择System.ComponentModel.DataAnnotations)
dotnet add package System.ComponentModel.Annotations
。 - Eric引用System.ComponentModel.DataAnnotations命名空间
在代码文件的顶部加入Using System.ComponentModel.DataAnnotations;,例如:
using System.ComponentModel.DataAnnotations;
通过在解决方案资源管理器中右键单击项目,向您的项目添加 .NET 引用:
希望这可以帮助您!这个问题对我很有帮助。
我之前也遇到过同样的问题,解决方法是在一个没有提到该引用的项目中添加该引用。如果您的解决方案中有2-3个项目,请尝试将此引用添加到其他项目中以查看是否可行。
[Display(Name = "My Property Name")].
不知道这是否适用于该.dll中的其他所有内容。]Microsoft Visual Studio Professional 2013
Version 12.0.21005.1 REL
Microsoft .NET Framework
Version 4.5.51641
已安装版本:专业版
使用框架版本4.5及以上版本进行项目,问题将得到解决。因为此命名空间在4.5及以上版本中。
System.ComponentModel.DataAnnotations包含在自己的程序集中,因此您需要确保已引用它。只需简单地执行以下步骤:
1). 右键单击解决方案,选择添加。
2). 从列表中选择引用。
3). 搜索“System.ComponentModel.DataAnnotation”,选中左侧的复选框,然后按ok。
完成,不应该有任何引用错误。
我从Silverlight 4升级到Silverlight 5,然后遇到了这个问题。虽然在我的项目的“引用”下有一个对“System.ComponentModel.DataAnnotations”的引用,但它旁边有一个黄色的警告标志,表示之前引用的程序集找不到。原来是因为“System.ComponentModel.DataAnnotations”引用的属性中指定了“Specific Version = True”,当我将其改为“Specific Version = False”时,问题就解决了。右键单击“引用”下的“System.ComponentModel.DataAnnotations”程序集,选择上下文菜单中的“属性”。检查“Specific Version”属性值是否为“False”。
在升级到Silverlight 5后,它可能仍然引用着旧的Silverlight 4程序集,而该程序集已不再可用。
System.ComponentModel
并使用其中的各种组件。根据下面@Hari的说法,似乎DataAnnotations
存在于它自己的程序集中。我原以为它会存在于System.ComponentModel
中,因为它和该命名空间相同。 - DaveDev