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 Web\Resources 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.Web\Resources\ValidationErrorResources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>ValidationErrorResources.resx</DependentUpon>
<Link>Web\Resources\ValidationErrorResources.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
Related
In my wpf project i have created a folder called practice, in that folder i added a window, now i want to run that window, so in app.xaml file i set the startup uri to foldername.window.xaml but it is saying build action property is not set to resource.
for that i setted build action property to resource. Now this time that it is showing error message initialized componenet doesn't exist in the current context.
Can you tell me what properties we need to set when we create separate folders in wpf project and that folders contains windows or pages. and How to access those pages in other pages or in App.Xaml file startupUri Property.
When you have folders in your project structure, you should use a "/" not a ".", so it's foldername/window.xaml.
(I hope it's not actually called window.xaml by the way. That's a confusing name for a type in a WPF project, because there's a built in type called Window.)
Setting the build action to Resource will make matters worse: not only were you using the wrong name, you've now changed the build action to the wrong one for XAML. The correct build action for a .xaml file is usually Page. (App.xaml is an exception to that rule.) The Page build action causes the page to be compiled into a binary representation (known as BAML), and that binary format can then be loaded either by the call to InitializeComponent in the codebehind, or through Application.LoadComponent.
Setting the build action to Resource will just embed a copy of the XAML source directly in the project, which won't help you - you can't work with XAML in that form if you want to have a codebehind file. (Not in WPF, anyway. It's different in other XAML-based frameworks such as WinRT.)
Since Page is the default build action for a newly-added window, you don't actually need to set any properties at all. You just need to use / for folder boundaries.
If the XAML is inside any Folder the startup url will be defined as below.
This is how it will defined.
I've got a WPF application I'm building. The solution contains a WPF control library project called CustomControls. There's a folder under the CustomControls project folder called Layouts. There's an XML file in that folder called OnscreenLayout.xml. The Build Action property for this file is set to Embedded Resource.
I'm trying to load that file into a stream in the code behind and pass the stream on to a method of a third party library class. The code in question looks like this:
OnscreenKeyboard.DefaultLayout = FPS.VirtualKeyboard.KeyboardLayout.Create(
App.GetResourceStream(
new Uri( #"/CustomControls;component/Layouts/OnscreenLayout.xml",
UriKind.Relative ) ).Stream );
When this code runs, it throws an IOException with the message
Cannot locate resource 'layouts/onscreenlayout.xml'.
I've even tried using this string for the Uri:
#"pack://application:,,,/CustomControls;component/Layouts/OnscreenLayout.xml"
But no joy. What am I doing wrong?
Edit: I have even tried changing the build action for the file from "Embedded Resource" to "Resource" and it still doesn't work.
Thanks
Tony
Only Content and Resource build actions are valid for resource files used in WPF application.
Please avoid other build actions such as Embedded Resource - this will work as well with appropriate API, but it is not favored by WPF.
I'm working on a Silverlight project with the WCF RIA Services beta. I'm using the BlahDomainService.metadata.cs file to validate a field by adding validation attributes, e.g. [RegularExpression]. It was working so I'm trying to put the Error message in a resource file and now it isn't working. The RegEx validation isn't being run on the client, though it is being run on the server.
Any idea what might be causing this?
In the generated code file on the client, I see this error:
// Unable to generate the following attribute due to the following error(s):
//
// - The validation attribute 'System.ComponentModel.DataAnnotations.RegularExpressionAttribute' declared ErrorMessageResourceName='RegExError' which was not found on declared ErrorMessageResourceType 'Blah.Web.Resources.SharedResources'.
// [RegularExpressionAttribute("yawn", ErrorMessageResourceName = "RegExError", ErrorMessageResourceType = typeof(Blah.Web.Resources.SharedResources))]
How I got where I am
I created the SharedResources.resx (and Vs created SharedResources.Designer.cs) file in the .Web project in a folder called Resources. In the Silverlight project, I created a Web folder and in that a Resources folder. To this Resources folder, I did Add > Existing Item and then added the SharedResources.resx and .Designer.cs using the Add as Link option. The idea was that this would keep the namespaces the same for the two resources classes. I then edited the .csproj file to make the .Designer.cs file a dependency of the .resx file, using the Silverlight Business Application template as a reference.
I built the solution and tested it and the RegEx validation throws no error, which is bad. Then I found the message above. To verify that my linking was working, in Home.xaml.cs, I typed:
System.Diagnostics.Debug.WriteLine(Web.Resources.SharedResources.RegExError);
and saw the error in the Output window in VS. I also submitted the changes to the service and in the EntitiesInError, on the VaidationErrors, I can see the error message, so I know it's working server-side. It's just the client-side that isn't. Any idea why it's not working?
You also have add a resources (.resx) as a link in the client silverlight project for the web project.
See template Silverlight Bussiness Application in VS 2010. It has a very good sample.
Everything was okay except that I hadn't set the AccessModifier for the .resx to Public. Once I'd done that and Rebuilt All, it worked.
I'm working on a Silverlight project with the WCF RIA Services beta. I'm using the BlahDomainService.metadata.cs file to validate a field by adding validation attributes, e.g. [RegularExpression]. It was working so I'm trying to put the Error message in a resource file and now it isn't working. The RegEx validation isn't being run on the client, though it is being run on the server.
Any idea what might be causing this?
In the generated code file on the client, I see this error:
// Unable to generate the following attribute due to the following error(s):
//
// - The validation attribute 'System.ComponentModel.DataAnnotations.RegularExpressionAttribute' declared ErrorMessageResourceName='RegExError' which was not found on declared ErrorMessageResourceType 'Blah.Web.Resources.SharedResources'.
// [RegularExpressionAttribute("yawn", ErrorMessageResourceName = "RegExError", ErrorMessageResourceType = typeof(Blah.Web.Resources.SharedResources))]
How I got where I am
I created the SharedResources.resx (and Vs created SharedResources.Designer.cs) file in the .Web project in a folder called Resources. In the Silverlight project, I created a Web folder and in that a Resources folder. To this Resources folder, I did Add > Existing Item and then added the SharedResources.resx and .Designer.cs using the Add as Link option. The idea was that this would keep the namespaces the same for the two resources classes. I then edited the .csproj file to make the .Designer.cs file a dependency of the .resx file, using the Silverlight Business Application template as a reference.
I built the solution and tested it and the RegEx validation throws no error, which is bad. Then I found the message above. To verify that my linking was working, in Home.xaml.cs, I typed:
System.Diagnostics.Debug.WriteLine(Web.Resources.SharedResources.RegExError);
and saw the error in the Output window in VS. I also submitted the changes to the service and in the EntitiesInError, on the VaidationErrors, I can see the error message, so I know it's working server-side. It's just the client-side that isn't. Any idea why it's not working?
You also have add a resources (.resx) as a link in the client silverlight project for the web project.
See template Silverlight Bussiness Application in VS 2010. It has a very good sample.
Everything was okay except that I hadn't set the AccessModifier for the .resx to Public. Once I'd done that and Rebuilt All, it worked.
I'm making a WebPart for SharePoint that will instantiate a Silverlight UserControl and feed it some data. My problem is that when I have created my sample-WebPart and just instantiate a Silverlight control, the webpart, when added to a page or displayed in the webpart gallery, instead of rendering blank, renders an error page saying "File Not Found". No clue in the logfiles to what file was not found or why this error is thrown. Here is my code:
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.SilverlightControls;
namespace TestSLWP {
public class CustomWebPart1 : WebPart {
protected override void CreateChildControls() {
Label lblHello = new Label();
lblHello.Text = "Hello";
Controls.Add(lblHello);
Silverlight sl = new Silverlight();
}
}
}
I've added references to System.Web.Extensions and System.Web.Silverlight to the project. They are in the GAC, and the webpart is written and compiled on the same computer that SharePoint resides. If I change the CreateChildControls() to be:
protected override void CreateChildControls() {
Silverlight sl = new Silverlight();
sl.ID = "CustomWebPart1SL";
sl.Source = "/Silverlight/CustomWebPart.xap";
this.Controls.Add(sl);
}
I get the same error. Also if I remove the first slash in sl.Source I get the same error, even though the file is present in a virtual directory in the same application pool as SharePoint. I therefore, and because the error comes with just instantiating the Silverlight object, believe that the file that cannot be found is not my XAP.
What file can't SharePoint find and what can I do about it?
Here's the error message:
http://www.freeimagehosting.net/uploads/2dca8dbdfb.png
Hi I found a complete walk through on how to get Silverlight web parts get running on your application: http://www.vbforums.com/archive/index.php/t-557072.html
As you can see there are added some more things to the web.config beside your assembly registration.
Enabling SilverLight requires a lagre amount of web config modifications. Did you add those?
There could be the problem with your storage folder with silverlight control. You must register path to this storage as safe in web.config (for example find in web.config line with "~/controltemplates").