English 中文(简体)
Localization of error message in Validation silverlight
原标题:

I want to use localization feature for Validation messages, for eg-

    [Required(ErrorMessageResourceName = "RequiredField", ErrorMessageResourceType = typeof( ))]
     public string someText
     { get... set...}

I m using MVVM pattern so this property is in my model(its a differnt project inside same solution of silverlight) and all my localization resources are in the App.current.Resources. How can I set the ErrorMessageResourceType to my App resources?

Please suggest.

Thanks in advance
Sai

问题回答

Well apparently Localization of error messages isnt as straightforward. You are supposed to add a resource file to the MyApp.Web project, that is the asp.net site that hosts your silverlight app, then add that resource to the silverlight app, then you will be able todo the code you stated in your question after some tweaks, follow the instructions below

This section explores how error messages can be localized by storing them in resource files and sharing them across tiers.

The example uses .NET RIA Services walkthrough project as the base project and builds on top of it.

Let s say we want to add a validation error as a resource for LoginID field.

  • Create a new ‘Resources folder in the HRApp.Web project (server project)

  • Add a new resource file to this folder and name it ValidationErrorResources.resx

  • Double click on the .RESX file to bring up resource designer page

  • Add a new string resource with Name= LoginIDValidationError and Value= "LoginID field is required"

  • Change the access modifier to ‘Public by clicking on the ‘Access Modifier drop down UI and selecting ‘Public and save the project. This generates a ValidationErrorResources class in the HRApp.Web.Resources namespace.

  • Open ‘OrganizationService.metadata.cs file and add the following ‘Required field validation to LoginID member. Specify the error message resource name and resource type values by setting the corresponding attribute members as shown below.

[Required(ErrorMessageResourceName = "LoginIDValidationError", ErrorMessageResourceType = typeof(ValidationErrorResources))]

public string LoginID;

Now we want to share this resource file in the Silverlight project (client project). To do this,

  • Create a folder WebResources in the HRApp project (folder structure must match the resource file namespace on the server side)

  • Select Resources folder and bring up Add Existing file dialog, browse to the server side resource file folder location

  • Select ValidationErrorResources.resx and ValidationErrorResources.designer.cs files, and add them as link files to the Silverlight project. Save the project file

  • Open HRApp.csproj file in notepad , locate the section where .designer.cs file is included and add the highlighted 3 lines to this section

   <Compile
 Include="..HRApp.WebResourcesValidationErrorResources.Designer.cs">

     <AutoGen>True</AutoGen>

     <DesignTime>True</DesignTime>

     <DependentUpon>ValidationErrorResources.resx</DependentUpon>

     <Link>WebResourcesValidationErrorResources.Designer.cs</Link>
 </Compile>
  • Save the project file and reload the project in Visual Studio

  • Build the solution and run

Now whenever the validation fails for the LoginID field the error message from the resource file is shown to the user. The resource file can now be customized to store locale specific error messages.

This solution almost worked for me. I had to made some arrangements to work with a data model (edmx) located in one project, DataDomainService (Ria) in other and the Silverlight access layer in other project. When i compile the HRApp equivalent in my situation, the metadata containing the validation info for some property is not generated. It says that the client has no access to the ValidationErrorResources type. But after following all the instructions mentioned above plus some others to get a correct resource namespace, the client CAN access ValidationErrorResources. It works if i write it myself to the generated Silverlight class. So seems like this kind of project separation is not quite supported by the class generator... But thanks anyway, this post was quite helpful and maybe i ll make it all work in a couple of days. :D

When I did this recently this thred helped alot: http://forums.asp.net/t/1433699.aspx

In particular "...the resource file must be converter to a class before being able to reference it in the typeof of the ErrorMessageResourceType in the data annotation..."

Also there are a few other useful hits from the main search engines: http://www.liquidjelly.co.uk/supersearch/?q=silverlight%20dataannotations%20localization&lang=en-GB





相关问题
Silverlight Rich text box control

Our team decided that we need our own custom Rich text box control for Silverlight app we are developing. We looked at existing controls mentioned at A good rich text control for Silverlight but ...

Silverlight ImageBrush not rendering (with Bing Map Control)

I m trying to add an image to a Pushpin instance from the Silverlight Bing Map Control, but I can t seem to get it to render (the pushpin renders fine). This is probably a general WPF question rather ...

Silverlight OpenFileDialog DoEvents equivalent

I m processing large files after they are selected by the user. My code looks like the following: if (FileDialog.ShowDialog() == true) { // process really big file } This freezes up the UI so ...

list of controls with templates in silverlight

Does anyone know where to find a list of controls that you can set the template on in Silverlight? I ve wasted several hours now trying to create control templates only to find that the control doesn ...

Silverlight, Updating the UI during processing

I have a simple silverlight multifile upload application, and i want to provide the user with some feedback, right now its only in a test phase and i dont have the webservice. Somehow i cant get the ...

Silverlight 3 - FindName returns null

This looks a bug to me.. Using Silverlight 3 and i have a user control defined in XAML and trying to access the object during runtime returns a null. <Grid> <common:CommonGridEditPanel x:...

silverlight 3 collection binding

Someone please help me understand why this binding does not work... I have a class called SelectionManager with a property called dates which is populated by a WCF service. The property is an ...

热门标签