Our client needed to include several video files in the dvd, loaded dinamically in a MediaElement in some of the pages. We didn't want to include these videos in the xap package by setting their Build Action as Content or Resource, because that made the xap file very big, and it really took a lot every time we built the project in Visual Studio. So I put them in a subfolder of the ClientBin directory in the "Web" project of the solution, with their Build Action set to "None".
Here's how solution explorer looked.
All I needed now was to reference them properly in the code, and I managed to do that by setting the source of the MediaElements to "/Assets/Video/[filename]
Then I compiled the project and copied the xap file and sllauncher.exe to a new folder on my desktop - you can find sllauncher.exe in your "\Microsoft Silverlight" subfolder in Program Files (or "Program Files (x86)" if you're working on a 64-bit version of the OS). Imagine the new folder is the root directory of the dvd: you also need to copy the videos in the correct subfolder, which for me was "\Assets\Video\". Notice there is no need of starting with "\ClientBin\", because when you build the output xap file is automatically put under ClientBin - it's like the root of the dvd IS your ClientBin.
Finally I created an Autoplay.bat to call sllauncher.exe with the parameters required. We needed the application to run directly from the dvd without installing, so I used /emulate along with the /origin and /overwrite parameters as explained in Tim Heuer's post. I also added an autorun.inf file to start Autoplay.bat automatically upon the insertion of the dvd, I will explain how to do it later in this post.
Everything seemed to be in its right place, but when I tried launching the Autoplay.bat it didn't work at all.
While developing the project I previously had to deal several times with the problems of changing the source of a MediaElement, and since most of the times the issue was path-related I guessed that was the case. But I necessarily had to use a relative path (that "/Assets/Video/
Finally we started asking ourselves if the problem was in the way I launched the application with sllauncher, and that was it. In particular, it was in the /origin parameter. This is a mandatory parameter, and you need to set the origin to a formally-correct URI, otherwise sllauncher will just fail execution with an error. The origin should actually be set to a real URI endpoint online: that way - even if I never tried this myself - the installed Silverlight application should update by itself anytime you publish an update to that URI.
So this is actually a really cool feature, but I didn't really need it. In my case, since the xap package location was on a dvd and there won't be any further updates for the application, I had linked the origin to a fake (but formally-correct) location online. What I didn't know is that the origin I specified when I invoked sllauncher.exe became the path of execution of the application, so my app was looking for the external assets by starting from the fake location I had entered, plus the relative path in the code! The whole uri of a single video must have been changed automatically into something like this:
"http://www.somewebsite.com/somefolders/.../Assets/Video/
So I changed the origin to "file:///[xapFile]
Now, to sum things up a bit... these are the steps I followed to make it work.
- Put your external assets under ClientBin in your ".Web" project (you can also create subfolders, in my case it was /ClientBin/Assets/Video/) and set their Build Action to "None".
- Refer to your assets in code with a relative path, assuming ClientBin is your root directory (in my case the path was "/Assets/Video/
- Try compiling and executing to be sure the assets are loaded correctly; if not, you have path issues that you should solve from within Visual Studio before proceeding further. You can find many resources online that can help you to achieve this.
- Create a new folder anywhere on your hard-drive, and copy-paste the content of the ClientBin folder from your ".Web" project into it - it should contain the generated xap file and your assets within their subfolders. Also copy-paste sllauncher.exe into it, and create two new files: "Autoplay.bat" and "autorun.inf".
- Edit "autorun.inf" to launch "Autoplay.bat" with the following code:
[autorun]
open = Autoplay.bat
- Edit "Autoplay.bat" to execute sllauncher.exe with the following code (remember to replace
sllauncher.exe /emulate:"[xapFile].xap" /origin:"file:/// [xapFile].xap" /overwrite
Your new folder should now look like this:
- Now create a cd/dvd image file with any cd/dvd burning program, and put the content of this folder into the root of the cd/dvd. Then mount the image file on a virtual dvd drive and everything should work fine.
Please note that if you launch the Autoplay.bat manually from your folder on the desktop it still WON'T work, because "file:///" relates to the root folder of the drive you're launching the application from. Suppose that the user's dvd drive is "F:", the path "file:///
I hope you found this useful, please leave comments or send me an email if you have any questions.
Giovanni Peroni.
Nessun commento:
Posta un commento