The Windows applications allow you to iteracte with Cortana at foreground and background. I believe that the background is what you are looking for:
First you need to create a XML file to represet your VCD (Voice Command Definitions). This is the place where you declare the command you want to use:
<?xml version="1.0" encoding="utf-8" ?>
<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.2">
<CommandSet xml:lang="en-us" Name="HomeControlCommandSet_en-us">
<CommandPrefix>HomeControl</CommandPrefix>
<Example>Control alarm, temperature, light and others</Example>
<Command Name="CheckTemperature">
<Example>Check temperature</Example>
<ListenFor>check [current] temperature</ListenFor>
<Feedback>Checking temperature</Feedback>
<VoiceCommandService Target="VoiceCommandService" />
</Command>
</CommandSet>
</VoiceCommands>
After you need to register the VCD at App.OnLaunched inside App.xaml.cs:
protected async override void OnLaunched(LaunchActivatedEventArgs e)
{
...
// Install the VCD
try
{
StorageFile vcdStorageFile = await Package.Current.InstalledLocation.GetFileAsync(@"HomeControlCommands.xml");
await VoiceCommandDefinitionManager.InstallCommandDefinitionsFromStorageFileAsync(vcdStorageFile);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("There was an error registering the Voice Command Definitions", ex);
}
}
After, you need to create a new Windows Runtime Component project and create a class that implements the IBackgroundTask:
public sealed class HomeControlVoiceCommandService : IBackgroundTask
{
private VoiceCommandServiceConnection voiceServiceConnection;
private BackgroundTaskDeferral serviceDeferral;
public async void Run(IBackgroundTaskInstance taskInstance)
{
// Create the deferral by requesting it from the task instance
serviceDeferral = taskInstance.GetDeferral();
AppServiceTriggerDetails triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;
if (triggerDetails != null && triggerDetails.Name.Equals("VoiceCommandService"))
{
voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);
VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();
// Perform the appropriate command depending on the operation defined in VCD
switch (voiceCommand.CommandName)
{
case "CheckTemperature":
VoiceCommandUserMessage userMessage = new VoiceCommandUserMessage();
userMessage.DisplayMessage = "The current temperature is 23 degrees";
userMessage.SpokenMessage = "The current temperature is 23 degrees";
VoiceCommandResponse response = VoiceCommandResponse.CreateResponse(userMessage, null);
await voiceServiceConnection.ReportSuccessAsync(response);
break;
default:
break;
}
}
// Once the asynchronous method(s) are done, close the deferral
serviceDeferral.Complete();
}
}
Don't forget to add this new project as a reference from the main one. Also, you need to register the service inside Package.appxmanifest:
<Extensions>
<uap:Extension Category="windows.appService" EntryPoint="CortanaComponent.HomeControlVoiceCommandService">
<uap:AppService Name="VoiceCommandService" />
</uap:Extension>
</Extensions>
This should work!
For a complete guide, you can check out this post