[WP7] Inject a file in a xap using post-build event
Let’s say that your application is dynamically loading resources, and you have to constantly add/remove new resources during the development. In this scenario, it could be a huge-time saver to just tell Visual Studio to take the contents of a folder and inject it into your application. I don’t think there’s an out-of-the-box way to do that, but you can inject the files yourself using a post-build event.
How? The output of every WP7 project is a .xap file, which is just a zip file with a different extension. So you can edit it using whichever zip extractor you like. In our case, let’s use 7zip.
First, create a basic application, and put an image control in the xaml:
1: <Grid x:Name="ContentPanel"
4: <Image x:Name="Image"
7: Source="Someimage.png" />
Make sure that the ‘someimage.png’ file does not exist in your project. Compile, run, and surely enough nothing is displayed.
Now, add the following line to the post-build event of your project: (right-click on the project, properties, “Build Events”)
1: "C:\Program Files (x86)\7-Zip\7z.exe" a -tzip $(ProjectDir)$(OutDir)Test.xap E:\someimage.png
- "C:\Program Files (x86)\7-Zip\7z.exe" is self explanatory: this is the path where you installed 7zip
- the “a” switch tells 7zip to add a file to the archive
- the “-tzip” switch forces the archive format to zip
- “$(ProjectDir)$(OutDir)” is automatically replaced by the output path of your project
- “Test.xap” is the name of the generated xap. Change it with the name of the file generated by your project
- “E:\someimage.png” is the path of the file(s) to inject
Rebuild the solution (to force Visual Studio to re-deploy the xap), then run the application, and your ‘someimage.png’ image should be displayed, even though it never had been added to the Visual Studio solution.
Hope that helps!
Ce post vous a plu ? Ajoutez le dans vos favoris pour ne pas perdre de temps à le retrouver le jour où vous en aurez besoin :