I’m currently working on a Windows Phone 8 project that requires two devices. Those devices talk to each other through a SignalR service. Thanks to Hyper-V I’m able to test/debug this project on two instances of the Windows Phone 8 emulator (I usually pick the WVGA and WVGA 512MB versions). What I used to do is deploy the XAP to one emulator and debug it on the second. I need to do this every time I change the app, I got bored of doing this manually so it was about time to get this automated.
Since Visual Studio 2012 builds the app everytime its codebase changes I can use the post-build script to do this, after all the app should only install itself on both emulators when it’s changed (I did spent time looking for a solution to let the script run every time I hit the debug button but it looks like there’s no way to do this in VS2012).
The point of the post-build script is to fire up the emulator when needed, deploy the app and run it. Visual Studio 2012 debug will fire up the second emulator, deploy the app there and launch it with debug, meaning that only one emulator will have a debugger attached.
Choosing the emulator for the script
Microsoft has provided a command line tool for deploying xap files, both onto a device and onto the emulators. Before we can start using it we’ll need to know the correct index of the emulator that we want the script to use. Copy-paste this script into a textfile and save as a .bat file, double click it and you should see a list of all available emulators on your machine (provided that you have the Windows Phone 8 SDK installed of course).
cd "%ProgramFiles(x86)%\Microsoft SDKs\Windows Phone\v8.0\Tools\XAP Deployment" XapDeployCmd.exe /EnumerateDevices pause
XapDeployCmd.exe is the commandline tool for everything related to XAP deployments. More information on the tool can be found on MSDN
Choose the emulator of your choose from the list and remember its index.
Building the script
Time to start building the script. In your Windows Phone 8 project, go to the project properties to the Build Events tab. In the Post-build event command line box enter this script.
cd %ProgramFiles(x86)%\Microsoft SDKs\Windows Phone\v8.0\Tools\XAP Deployment XapDeployCmd.exe /installlaunch $(TargetDir)$(ProjectName)_$(ConfigurationName)_$(PlatformName).xap /targetdevice:1
The first line of the script navigates the command line to the folder where the XapDeployCmd lives (this is the default install path, the tool comes with the WP8 SDK). The second line launches the tool and passes the necessary parameters. the installaunch parameter states that the app should install (or update) the app and launch it when finished. After the installaunch parameter we need to state the path to the xap file, by default (when using the debug configuration in VS2012) it’s in the bin/debug folder and is called something like MyWPApp_debug_anycpu.xap. To make the script easy to transfer over I’ve used variables instead of hardcoding the path and the xap name. Here’s a quick rundown.
|$(TargetDir)||the full path to the outputdirectory (for example: c:\users\nico\documents\Projects\MyWPApp\bin\debug\)
Keep in mind that the last “\” is always included when using this variable
|$(ProjectName)||The name of the project (didn’t see that one coming, did you? ) (for example: MyWPApp)|
|$(ConfigurationName)||The used configuration, for example ”release” or “debug”|
|$(PlatformName)||The selected CPU architecture, for example “AllCpu”, “ARM”, “x86”|
variables combined we get the full path to the xap file. The last parameter specifies the to use emulator by passing in the index that we determined at the beginning of this post.
And that’s it. Rebuild your solution and watch the emulator start app, deploy and launch your app. Now every time that you change the code of the app and hit the debug button it will build and deploy to both emulators. One of the emulators should have those debug numbers on the side, making it easy to recognize which one has the debugger attached.
Using a very simple post-build script and the in the WP8 included XapDeployCmd tool it’s very easy to deploy an application to two devices simultaneously. This isn’t needed very often but when building something that connects users to each other (like a chat application or a multiplayer game) it can save you quite some hassle. Just make sure that your script uses another emulator version than Visual Studio 2012.