Silverlight Asp.Net project integration - silverlight

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

Related

How to use a Silverlight Solution

I have a Silverlight app made in Visual Studio 2010 and I want to put it on a website but I don't have the slightest clue how.
I've looked at all the msdn documentation and they all mention a .xap file that is the file you use on the website but there is no .xap and I can't find out how to compile my code into a .xap file.
Also, when I run the App and look at the source in the browser, it has my code compiled into the .xap file, but there is no .xap file!
So my question is simply how do you go about getting a Silverlight app on a website because I've been trying for hours and I can't figure it out.
A sample html or aspx page is included when you create the solution. This is the page that the browser navigates to when you hit F5. You can take that page as a basic example of how to embed a Silverlight app in a page. You should find it in the project folder.
Silverlight projects are commonly built into a XAP file when you hit "Build" in VS. This file lives in the bin/Release or bin/Debug folder and basically contains your whole application.
Steps to create a silverlight application Hosted in a web site,
Select Silverlight Application Template while adding new project in VS2010
When you click Ok, ensure that Host the Silverlight application in a new web site is Checked.(It will create a new ASP.Net web application to host the Silverlight)
If you haven't checked the Host the Silverlight application in a new web site option, you can add a new ASP.Net Web application project and then go to its properties and select Silverlight Applications option in left pane. Then Click Add, select your Silverlight Application to be hosted and click ok. You are done now.
But If haven't selected the Silverlight Application Template, then you might have created a Silverlight Class Library. It wont generate any xap file. It just gives a dll. So you need to recreate a project as mentioned above.
I believe that in Silverlight whenever you create a project, right at the beginning, it will ask you if you want to create a web app automatically. If you chose yes, something like YOUR_PROJECT_NAME.web will be created, go into that folder, you will find a folder called ClientBin.
The .xap file will be inside that folder.
HTH

Changing Silverlight Application name dynamically

I'm creating a deployable module where some parts are written in Silverlight, and I'm making the SL application deployable for OOB usage. However, I want to make Silverlight take the name of the website that it's deployed from, such as, when a user installs it from Example.com, I want to have "example.com application" with the site's own icon in the shortcut. Is there any "supported" method of doing this, or will I be going with locating the XAP file and manually changing AppManifest.xaml inside it?
You will need to find out your URL of your application:
string appURL = Application.Current.Host.Source.AbsoluteUri.Substring(0, Application.Current.Host.Source.AbsoluteUri.IndexOf(#"ClientBin/"));
So this will solve the title problem, next is the icon. You could load the image from the page:
Uri uri = new Uri(String.Format("{0}/favicon.png", appURL));
IconImage.Source = new BitmapImage(uri);
It's not perfect, you will have to manipulate appURL to get the domain name only.

WPF installation

I'm new to WPF, and created a 1st simplistic WPF application that I want it to run in a webbrowser, IE or Fox.
1 - Within the Visual Studio project, I created a /Images folder with a few .jpg files
On the WPF xaml form I have 1 image and 1 button.
When application starts, the image displays /Images/img1.jpg
When User clicks the button the image must display /Images/img2.jpg
How can I force the VS publisher to include the Images folder? Apparently I can't see it in the ApplicationFiles ?
2 - Though I was able to program and run this small app on my local computer, I'm getting lost when it comes to deploying to my hosting ASP where I have a Windows hosting account that runs .NET 3.5!
From Microsoft WPF website they say I shall deploy 3 files:
"The Application Executable .exe
The Deployment Manifest .xbap
The application Manifest .manifest
The .xbap file contains the information that ClickOnce uses to deploy the application and has the .xbap extension."
But I can's see no .xbap files at all within the published stuff!!!
Any clue please?
To answer your first question:
The images will be embedded in the compiled application, so you will not see the images in the application files.
Note:
The build action of images added to your project is 'Resource' by default - leave this as is.
Do not use the resources tab in the Project properties window, just drop and drag the images into the Images folder in the solution explorer.
To help answer your second question:
Did you create the project as an WPF Browser Application?
In your .csproj file you should see the following:
<HostInBrowser>true</HostInBrowser>
<Install>False</Install>
<ApplicationExtension>.xbap</ApplicationExtension>
<TargetZone>Internet</TargetZone>
If not, just recreate the project as a WPF Browser Application and copy your files from the existing project to the new one.

silverlight - open xaml in new web browser

In my silverlight application I want to open a xaml in a new web browser. how can I do that?
with HTMLPage.Window.Navigate it is just aspx pages - maybe I can convert xaml to aspx?
10x!
a XAML page cannot be shown in a browser window. What Silverlight do actually is showing the Silverlight program within an < object > tag in the aspx (or html) page; nothing more.
In your silverlight project, if you chose to host your project in a new web site, you will see a second project with both a htm and aspx file that hosts that object that links to your xap file.
So basically, you need to create a second Silverlight project that will be hosted in a different aspx page. Then in your main silverlight project your can open that new aspx page in a new web browser window.

Silverlight client does not get copied on to the Silverlight Web project clientBin folder

Hi When trying to run the Build Locally Silverlight client files does not get copied on to the Silverlight web's ClientBin folder, any idea what to do? The Version of Silverlight is 3.0
More information needed. you can first go to you web site property to check if you set the silverlight application path to clientbin folder.
Go to the properties of the web application.
There is a group called "Silverlight Application"
Click the Add button
Select "Use an exisiting Silverlight project in the solution" and select the Silverlight project from the drop down.
Make sure that the Destination folder field is set to ClientBin.

Resources