Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

May 3, 2011

Silverlight Validation: Resource File

Filed under: ASP.NET RIA Services,C#,Silverlight @ 2:04 am

Resource files are a great way to manage strings, especially if you need to support multiple languages. This post demonstrates how to use a resource file with the validation attributes used by Silverlight and WCF RIA Services.

NOTE: This post is part of a set of validation posts which starts here and it uses the sample code from this prior post.

In this prior post, the code example demonstrated how to use the single property validation attributes available in Silverlight and WCF RIA Services. For example, to validate that a first name is entered and does not exceed 20 characters, the attributes look like this:

[Required(ErrorMessage="Please enter a first name.")]
[StringLength(20, ErrorMessage = "First name cannot exceed 20 characters.")]
public string FirstName { get; set; }

Notice how the validation error messages are hard-coded strings. This poses a challenge if you need to localize your application.

A better option is to add the strings to a resource file. The trick here is that the resource file needs to exist both on the server (in the Class Library project if you are using WCF RIA with your own business layer) and on the client (in the Silverlight project). This is done by a process called linking.

1) In the Class Library project containing your business layer, add a resource file (Add | New Item | Resources File).

I added mine to a Resources folder in the project.

NOTE: If you are not using your own business layer on the server, you can add the resource file directly to the Web project instead.

2) Enter a name for each string along with the string values. Optionally add a comment.

3) Set the Access Modifier for each resource string to Public.

The result should look something like this:

image

4) In the Silverlight project, link to that resource file by selecting Add | Existing Item and then clicking on Add As Link in the Add Existing Item dialog.

image

5) In the Silverlight project, link also to the associated Designer.cs file.

NOTE: The Add Existing Item dialog allows multiple selection. So you could link both the rex file and the Designer/cs file in one step.

6) Modify the attribute parameters in the business layer Class Library project to reference the resource file instead of using hard-coded strings. Be sure to add a using for the resource file’s namespace:

using InStepValidationExample.BL.Resources;

[Required(ErrorMessageResourceType = typeof(ValidationErrorResources),
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). The ErrorMessageResourceName parameter specifies the name that you defined for the associated resource string in the resource file.

7) Now for the tricky part. 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.Web project under a Resources folder. The resources are then in the BizApp.Web.Resources namespace.

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.

image

In the example for this post, however, the resources are in a Class Library in a business layer separate from the Web project:

image

The namespace of the resources in the business layer is InStepValidationExample.BL.Resources. The namespace of the resources in the Silverlight project is InStepValidatoinExample.SL.Resources.

So if you run the application at this point, it will crash with a "TargetInvocationException occurred" message. 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 on the Silverlight project and select Unload Project.

7b) Right-click on the Silverlight project again and select to edit the project file.

7c) Add a <LogicalName> element to the <EmbeddedResource> element in the project file and set it to the namespace and resource name. Mine looked like this:

<ItemGroup>
  <EmbeddedResource Include="..\InStepValidationExample.BL\Resources\ValidationErrorResources.resx">
    <Link>Resources\ValidationErrorResources.resx</Link>
    <LogicalName>
  InStepValidationExample.BL.Resources.ValidationErrorResources.resources
     </LogicalName>

  </EmbeddedResource>
</ItemGroup>

7d) Close the project file.

7e) Right-click on the Silverlight project and select Reload Project.

Now when you run, it should find your resource file.

image

Use a resource file any time you want better management of your strings, especially if you plan to localize your application.

Enjoy!

7 Comments

  1.   Silverlight Blog — May 5, 2011 @ 7:44 am    Reply

    You demonstrates how to use a resource file with the validation attributes used by Silverlight.I think it would be very helpful for silverlight developers.

  2.   cruz — May 10, 2011 @ 6:56 pm    Reply

    when i use a resource file, the validation only occurs on click button, it’s correct?

  3.   Leo — May 13, 2011 @ 3:05 pm    Reply

    (sorry for my bad english)
    Thanks, I love your posts, always detailed and well written. Can you show how to create and test a Resource File to add another language? I have it working with one language the application get the string from the file.resx file but i do not know how to make it work with another languages… tried to create a file.pt-BR.resx (just copied the file.resx, renamed it and translated the strings) and on App.xaml.cs I set

    Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo(“pt-BR”);

    It generates a resources.dll inside my debug/pt-BR folder, but the application dont show the strings that i defined there :/

    How my silverlight client should get the resource strings? They all are embebedd inside the xap? There is a way to debug it to see where the application is getting those strings?

  4.   Sandeep — July 12, 2011 @ 1:25 am    Reply

    Thanks a ton. You saved my day. 🙂

  5.   RD — October 26, 2011 @ 2:37 pm    Reply

    http://msmvps.com/blogs/deborahk/archive/2011/05/03/silverlight-validation-resource-file.aspx

  6.   Pradeep Kumar — June 7, 2012 @ 4:53 am    Reply

    great work and explained using real practical scenario. Thanks a lot for sharing such nice and precise information.

  7.   julio — September 4, 2012 @ 7:27 am    Reply

    Thanks! 🙂

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2018 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs