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

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.

Related

correct way to add js in sencha architect (ExtJS)?

This thread works well in a mobile app but in a desktop app it would seem I cant add the reference to the external JS in an extjs desktop app.
The instructions from Sencha somehow don't correspond or it doesn't work for me when I try and follow them. So, I am selecting Resources->Library and can see the attribute Include JavaScript (ticked) and Library Base Path:/ext.
The JS file I am using I use in a phone app and its fine - I added it to the app.json in that and compiling the app copies the file over to the target and defers the loading.
In this case with SA I am not seeing the file copied to the target - nor can I follow the instructions with SA 3 that are documented. The only way I can add an external JS is by dropping it onto the filesystem into the ext folder manually. Again, it doesn't appear in the target and certainly errors when I run the app with
Uncaught ReferenceError: hex_sha512 is not defined
This JS has been used in other apps, is proved and tested but just relates to SA or my wrong use of SA.
The proper way to add a resource in Sencha Architect, regardless of framework, is to add it as a JS resource.
This is most easily done by hitting ctrl-t (or cmd-t on a mac) and typing "js resource" (or some shortened string thereof to get autocompletion)...
Alternatively, you can use the toolbox, click "Resources", and drag out or double click "JS Resource"
These are just different user interfaces to accomplish the same task.
Once you've added your JS resource using one of the above methods, you need to set the URL in the config pane (at bottom right unless you've configured Architect to swap the left and right panes)...
The URL is to be set relative to your project folder. If you copy the JS file into your project root under a folder called "lib" for example, then you'd set the url config to "lib/foo.js" - where foo.js is the filename, of course.
Hope that helps!
My lack of understanding of the instructions or they are not clear:
It would seem the way to do it is take eyes up to the top right of SA and spot the + button as shown in the image.
Add the JS resource and scroll up because it may be hidden behind the property inspector.
On setting the url field under properties, the source of the JS appears in the main editing window.
Certainly works fine after the app is built.

Can't get pack Uri to work

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.

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

Silverlight PRISM loading an external XAP module

I'm not sure if this is possible, but I am currently loading most of my modules from within the main application assembly, I am trying to now load external XAP modules.
I have something like this:
ModuleInfo themeModule = new ModuleInfo();
themeModule.ModuleName = "Theme_External";
themeModule.ModuleType = "Theme_External.Theme_External_Module, Theme_External, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
themeModule.Ref = "file://Theme_External.xap";
themeModule.InitializationMode = InitializationMode.OnDemand;
catalog.AddModule(themeModule);
I then realised that you cannot use that ref as it uses local file access which is not available in Silverlight. The only reason this was working for me is I had a direct reference to the theme project which I needed to remove. I'd like to either point it at say http://localhost/Theme_External.xap or a remote web address later down the line.
Is this something I can do with the current PRISM set up?
Thanks for your time
What Dave said...
and in case it isn't obvious, the only change you'd need to make (assuming that your xap files are in the same directory) is to set the ref property to:
themeModule.Ref = "Theme_External.xap";
Reletive urls will work if it's in a directory lower than your shell xap file. If not, you'll need the absolute url.
You can download XAP files on demand as long as they are in the same domain as the primary application. There is a comprehensive article on Dynamic Data Delivery in Silverlight here.
If you want to download a XAP file hosted on a different domain, you'll have to have a Cross-Domain Policy file on that domain.

Resources