I work on the team that owns Windows Client App Tooling in Visual Studio and we are always on the lookout for improvements we can do to the developer workflow. One day, My manager (Unni) asked if I have ever tried to debug a Cortana launch sequence in UWP apps. My obvious answer was no. So, I went and dug around a little bit.
As you can imagine, debugging app launch sequences when not explicitly triggered by the developer, is tricky. There is not enough time to attach a debugger before your launch code gets executed. You will either have to fake extra long loops, or try to add Debugger.Break() (On an added note, this doesn't seem to work all that well in UWP) to your code and mess it up. Imagine my surprise, when I found this nifty little method with the help of our debugger team.
To follow along, you will want to download the sample Cortana Project here.
Installing the app
Download the CortanaLaunch project, unzip and open the solution in Visual Studio. Now either F5, or use the debug toolbar to launch the application. If everything worked as expected, you should be able to see the below view. This step is also important because we have code in the project to install the Voice Command Definition file that is required for Cortana Activation. For a step by step tutorial for creating a simple Cortana integration for a foreground app, check out the MSDN article here.
Setting up the app to debug when launched
Open the project in Visual Studio and bring up the project properties page. You can do this by either right clicking the project in the Solution Explorer and invoking the Properties command or you can do the same from the Project Menu Item.
Navigate to the Debug page and you will see a new option
"Do not launch, but debug my code when it starts"
Make sure this option is set and then go ahead and start debugging the app (F5). At this point you will notice that VS is in debug mode, but the app has not launched yet. Lets see if the option lives up to its name.
Open App.xaml.cs and put a breakpoint on Line #71 in the OnLaunched and on Line # 33 on the OnActivated methods.
Now start the app from the Start Menu by searching for Cortana Launch.
As soon as the app is launched, you can see that Visual Studio attaches the debugger to it and the breakpoint that you had set is hit.
Now to test out Cortana launch, you can restart debugging and say the following phrase with Cortana activated
"Sample, show start page"
The debugger will again attach to the process and the second breakpoint on OnActivated is hit. Voila, you are now successfully able to debug your app code in the cortana launch code path.
As you can see, this is a nifty way to debug cortana launch or other app activations. If you have questions, comments or feedback, comment below or tweet me directly.