Silverlight验证之资源文件

资源文件是一个伟大的方式来管理字符串,特别是如果你需要支持多种语言。,这里演示了如何使用资源文件与验证,所使用的Silverlight WCF属性(RIA)服务。
代码例子演示怎样使用单体验证属性可在Silverlight WCF(RIA)和服务。举个例子,表明一个名字不超过20个字符,属性这个样子的:
[Required(ErrorMessage="Please enter a first name.")]
[StringLength(20, ErrorMessage = "First name cannot exceed 20 characters.")]
public string FirstName { get; set; }
注意你的验证错误信息被硬编码字符串。.这造成了一种挑战,如果你需要定位您的申请。
一个更好的选择是将字符串资源文件。. 技巧在于:这个资源文件需要在服务器上存在两类库项目(如果使用的是WCF(RIA)与你自己的事务层)和在客户端(Silverlight项目)。这是由这一过程被连接。
1) In the Class Library project containing your business layer, add a resource file (Add | New Item | Resources File).一)类库项目包含有你的业务层,增加一个资源文件(添加| |资源新项目的文件)。
I我加了矿井资源的文件夹在这个项目。
.注:如果你不使用您自己的业务层在服务器上,你可以添加资源文件中的直接到网页专题研究计划来代替。
2) 输入一个名字每一串随着串值。Optionally add a comment.选择性地增加评论。
3) Set the Access Modifier for each resource string to Public.3)为每个资源的访问修饰字串是公开的。
The result should look something like this:结果应该看起来像这样:

Silverlight验证之资源文件

4) In the Silverlight project, link to that resource file by selecting Add | Existing Item and then clicking _disibledevent=>
Silverlight验证之资源文件

5) In the Silverlight project, link also to the associated Designer.cs file.5)在Silverlight项目,连接同样相关的Designer.cs文件。
NOTE: The Add Existing Item dialog allows multiple selection. 注:增加现有的项目对话框允许多重选择。So you could link both the rex file and the Designer/cs file in _disibledevent=>
ErrorMessageResourceName = "FirstNameRequired")]
[StringLength(20,
ErrorMessageResourceName = "FirstNameLength",
ErrorMessageResourceType = typeof(ValidationErrorResources))]
public string FirstName { get; set; }
Instead of setting the ErrorMessage parameter to a hard-coded string, this code sets the ErrorMessageResourceType parameter to the type of the resource file (which is the resource file name). ErrorMessage而不是设置的参数到一个硬编码的字符串,该法规规定ErrorMessageResourceType参数对该类型的资源文件(即为资源文件的名称)。The ErrorMessageResourceName parameter specifies the name that you defined for the associated resource string in the resource file.这个参数指定了ErrorMessageResourceName名你定义为相关的资源字符串在资源文件中的值一样。
7) Now for the tricky part. 7)了,这是棘手的问题。By default, Visual Studio requires that the two resource files be in the same namespace. 默认情况下,视觉工作室要求这两种资源文件在同一命名空间。If they aren't, the application won't find the resources.如果没有,应用找不到的资源。
For example, if you use the Silverlight Business Application template, you can see that it puts the resource file in the BizApp.举例来说,如果你使用了Silverlight业务应用模板,你可以看到,它将在BizApp资源文件。Web project under a Resources folder. 下一个资源网页专题研究计划的文件夹。The resources are then in the BizApp.Web.Resources namespace.资源是那么BizApp.Web.Resources命名空间。
To access the resources from the BizApp Silverlight project, the file is put in a folder hierarchy with a Web folder and then a Resources folder so the resulting namespace is again BizApp.Web.Resources.访问资源,从BizApp Silverlight项目,文件被放在一个文件夹层级和网络文件夹,然后一个资源目录中再次BizApp.Web.Resources产生的命名空间。

Silverlight验证之资源文件

In the example for this post, however, the resources are in a Class Library in a business layer separate from the Web project:在例子中担任这个职务,然而,资源是在一个类库在一个事务层分离的网页专题研究计划:

Silverlight验证之资源文件

The namespace of the resources in the business layer is InStepValidationExample.BL.Resources. 名称空间资源是InStepValidationExample.BL.Resources业务层。The namespace of the resources in the Silverlight project is InStepValidatoinExample.SL.Resources.名称空间的资源InStepValidatoinExample.SL.Resources Silverlight项目。
So if you run the application at this point, it will crash with a "TargetInvocationException occurred" message. 因此,如果你运行的应用在这一点上,它将会坠毁与“TargetInvocationException发生”的消息。If you look at the exception detail it says "Could not find any resources appropriate for the specified culture or the neutral culture..." 如果你看看它说:“除了细节也没能找到任何资源适当为指定的文化或中性的文化……”Basically, it cannot find the resource file.基本上,它不能找到资源文件。
Luckily, we know the secret handshake required to get this to work:幸运的是,我们知道这个秘密的握手要使这项工作:
7a) Right-click _disibledevent=>
<Link>Resources\ValidationErrorResources.resx</Link>
<LogicalName>
InStepValidationExample.BL.Resources.ValidationErrorResources.resources
</LogicalName>

</EmbeddedResource>
</ItemGroup>
7d) Close the project file.7d)关闭的项目文件。
7e) Right-click _disibledevent=>
Silverlight验证之资源文件

使用资源文件任何时候你想更好的管理你的字串,特别是如果你计划去定位您的申请。Enjoy!享受吧!
Tags: 

延伸阅读

最新评论

发表评论