Silverlight PRISM loading an external XAP module - silverlight

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.

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.

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.

Silverlight 4 OOB application and Images folder

I am developing a Silverlight 4 application which displays items in a carousel control. The items are parsed form an XML file and then loaded as an image which can be double clicked to open a website, file, image etc.
I am already running in higher privileges mode (out of browser) to access the Xml file which sits in a subfolder of the application.
I can load the images fine when they are added to the project as they are included as resources. However, I need to be able to load images which aren't individually added to the project. Instead, I would like to add all images in a particular folder as resources.
Is this possible?
Thanks in advance
From Advanced Silverlight Out of Browser- Introduction
When running in a trusted environment,
you can access only files in user
folders, specifically the MyDocuments,
MyMusic, MyPictures, and MyVideos
folders
So, if you want to access folders other than this, you would probably need to run some sort of service, which has the proper rights. What kind of service depends on where this app is running. Could be a WCF service, or a simple WebService, if you can run a webserver on the machine. Otherwise, you'd need a windows service of some sort.

Silverlight Asp.Net project integration

I added a Silverlight application to my ASP.NET website. Visual Studio made a new silverlight project and added its xap to the ClientBin folder under the project of my website. So both the projects are under one solution.
My Silverlight app is supposed to read an xml file and I was unable to make it access the file from the client bin folder under the website project. Adding a reference to that project does not work since it says only references to other silverlight applications can be added. Right now its working when the file is under the silverlight project but not when it is under the website project.
how can I make it read the file from website project?
The project structure is
WEBSITE1 (solution)
-WEBSITE1 (project)
-ClientBin
-file0.xml
-silverlightchart.xap
-SilverlightChart
-file1.xml
I can access file1.xml using
XDocument document = XDocument.Load("file1.xml");
I want to access file0.xml but no path works for me, for e.g,
XDocument document = XDocument.Load("~/ClientBin/file0.xml");
and WEBSITE1 is the startup project
You should be able to read a file from the ClientBin folder simply enough without needing to do anything special. At a guess I would say the you have accidentally set the Silverlight application as the startup project. In this scenario you want the website to be the startup project then either have the Silverlight apps test page marked as the start page or to navigate to the silverlight page once the browser has started.
Edit
The problem you have is that the Load method is synchronous but Silverlight does not support synchronous access to web resources. Hence passing a Uri to the Load method will only work if the that Uri can be fulfilled by content in the Xap. Thats why an Xml file in the silverlight project works because it ends up in the Xap.
To fetch Xml from the site you need to do this:-
WebClient client = new WebClient();
client.DownloadStringCompleted += (s, args) =>
{
XDocument document = XDocument.Load(args.result);
SomeFunctionToContinueWithDocumentProcess(document);
}
client.DownloadStringAsync(new Uri("file0.xml", UriKind.Relative);
// code exits here but _document won't be loaded yet

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

Resources