Bienvenue à Blogs CodeS-SourceS Identification | Inscription | Aide

[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"
   2:       Grid.Row="1"
   3:       Margin="12,0,12,0">
   4:     <Image x:Name="Image"
   5:            Width="300"
   6:            Height="300"
   7:            Source="Someimage.png" />
   8: </Grid>

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!

Publié jeudi 12 janvier 2012 14:47 par KooKiz
Classé sous : , , , ,
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 :

Commentaires

Pas de commentaires
Les commentaires anonymes sont désactivés

Les 10 derniers blogs postés

- Etendre le Team Web Access de TFS 2012 – Step 0 par Philippe Didiergeorges Aka Philess le il y a 17 heures et 11 minutes

- Simuler facilement l’envoi de mail par Blog de Jérémy Jeanson le 05-22-2013, 12:52

- ProcDump 6.0 : support du filtrage sur messages d'exceptions .NET, des filtres multiples et du ciblage par nom de service par CoqBlog le 05-20-2013, 14:50

- Votez pour le TOP 10 des influenceurs SharePoint francophones ! par Le blog de Patrick [MVP SharePoint] le 05-20-2013, 12:59

- [Conf’SharePoint] Dernier rappel ! :-) par Le blog de Patrick [MVP SharePoint] le 05-20-2013, 09:09

- [ #SharePoint 2013 ] les modèles de sites standards… par Le blog de Patrick [MVP SharePoint] le 05-20-2013, 09:03

- 10 erreurs de compréhension concernant SharePoint… par Le blog de Patrick [MVP SharePoint] le 05-20-2013, 08:27

- Conf’SharePoint : 10 bonnes raisons pour ne pas la rater par Le petit blog de Pierre / Pierre's little blog le 05-14-2013, 02:24

- [Event] Soirée de lancement Agile .NET France à Lyon par Blog Agile/ALM de Vincent THAVONEKHAM le 05-13-2013, 01:29

- .NET / Debug : inspection de la mémoire d'applications .NET (dump ou processus live) : première livraison d'une librairie .NET par Microsoft par CoqBlog le 05-11-2013, 22:21