Can't get pack Uri to work - wpf

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.

Related

Visual Studio: adding image as resource vs. simply adding the file

What is the difference between adding an existing image (.png) as a resource and simply adding the file to the solution? Which is to be preferred, in general and especially as a button content?
A build action of 'Resource' embeds the image in your assembly so that it doesn't need to be shipped as a loose file with your application. A build action of 'Content' adds the image to the manifest so the application knows about it and expects it to be present, but doesn't embed the image--you need to deploy the file separately, and the application will search for it at runtime. Both resources and content can be referenced with relative and absolute pack: URIs.
A build action of 'None' does nothing. If you include the file with your application, you won't be able to reference it with a pack: URI unless you use the absolute URI form with a siteoforigin:,,, authority. It's best not to do this, especially since the behavior changes when your application is deployed with ClickOnce.

how to set startup uri property to another window in folder in wpf project

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.

How can I reference resx files in a Silverlight RIA Services Library? [duplicate]

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.

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 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

Why isn't my shared resource file working in my Silverlight RIA project?

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.

Resources