Cortana Command demonstrates how to launch an application with Cortana with Voice Commands
Step 1
If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu find and select Visual Studio 2017.
Step 2
Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…
Step 3
From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in a Name and select a Location and then select Ok to create the Project
Step 4
Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10 Fall Creators Update (10.0; Build 16299) and the Minimum Version to be the same.
The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates
Step 5
Once done select from the Menu, Project, then Add New Item…
Step 6
From the Add New Item window select Data from Installed and then select XML File from the list, then type in the Name as VCD.xml before selecting Add to add the file to the Project
Step 7
Once in the Code View for VCD.xml after <?xml version=”1.0″ encoding=”utf-8″ ?> the following should be entered:
<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.2"> <CommandSet xml:lang="en-us" Name="ColourCommandSet_en-us"> <AppName>Colour</AppName> <Example>Show Red</Example> <Command Name="showColour"> <Example>Show Corn Flower Blue</Example> <ListenFor RequireAppName="BeforeOrAfterPhrase">Show {colour}</ListenFor> <Feedback>Showing {colour}</Feedback> <Navigate Target="foo"/> </Command> <PhraseTopic Label="colour"/> </CommandSet> <CommandSet xml:lang="en-gb" Name="ColourCommandSet_en-gb"> <AppName>Colour</AppName> <Example>Show Red</Example> <Command Name="showColour"> <Example>Show Corn Flower Blue</Example> <ListenFor RequireAppName="BeforeOrAfterPhrase">Show {colour}</ListenFor> <Feedback>Showing {colour}</Feedback> <Navigate Target="foo"/> </Command> <PhraseTopic Label="colour"/> </CommandSet> </VoiceCommands>
Step 8
Once done select from the Menu, Project, then Add New Item…
Step 9
From the Add New Item window select Visual C#, then Code from Installed then select Code File from the list, then type in the Name as Library.cs before selecting Add to add the file to the Project
Step 10
Once in the Code View for Library.cs the following should be entered:
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text.RegularExpressions; using Windows.ApplicationModel; using Windows.ApplicationModel.VoiceCommands; using Windows.Storage; using Windows.UI; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Shapes; public static class Library { private static IEnumerable<string> SplitCapital(string text) { Regex regex = new Regex(@"\p{Lu}\p{Ll}*"); foreach (Match match in regex.Matches(text)) { yield return match.Value; } } private static Dictionary<string, Color> _colours = typeof(Colors) .GetRuntimeProperties() .Select(c => new { Color = (Color)c.GetValue(null), Name = string.Join(" ", SplitCapital(c.Name)) }).ToDictionary(x => x.Name, x => x.Color); public static string Command { get; set; } public static async void Parse(TextBlock title, Rectangle display) { try { StorageFile file = await Package.Current.InstalledLocation.GetFileAsync(@"VCD.xml"); await VoiceCommandDefinitionManager.InstallCommandDefinitionsFromStorageFileAsync(file); } catch { } if (!string.IsNullOrEmpty(Command)) { string titleCase = Regex.Replace(Command.ToLower(), @"(^\w)|(\s\w)", m => m.Value.ToUpper()); if (_colours.Any(a => a.Key == titleCase)) { KeyValuePair<string, Color> value = _colours.Where(w => w.Key == titleCase).FirstOrDefault(); title.Text = value.Key; display.Fill = new SolidColorBrush(value.Value); } } } }
In the Library.cs Class which is declared as static there are using statements to include the necessary functionality. There is a SplitCapital Method which is used to normalise the name of a colour so it appears split by capital letters, such as CornflowerBlue appearing as Cornflower Blue. There is a string Property for a Command and Parse Method which will InstallCommandDefinitionsFromStorageFileAsync from the provided VCD.xml and will be used to interpret compatible Commands and used to get the name of a Colour that was spoken in the form of a Command.
Step 11
In the Solution Explorer select App.xaml
Step 12
From the Menu choose View and then Code
Step 13
Once in the Code View below private void OnSuspending(object sender, SuspendingEventArgs e) { … } the following should be entered:
protected override void OnActivated(IActivatedEventArgs e) { base.OnActivated(e); try { if (e.Kind == ActivationKind.VoiceCommand) { VoiceCommandActivatedEventArgs args = (VoiceCommandActivatedEventArgs)e; Library.Command = args.Result.SemanticInterpretation.Properties["colour"].FirstOrDefault(); } } catch { } Frame rootFrame = Window.Current.Content as Frame; if (rootFrame == null) { rootFrame = new Frame(); rootFrame.NavigationFailed += OnNavigationFailed; Window.Current.Content = rootFrame; } if (rootFrame.Content == null) { rootFrame.Navigate(typeof(MainPage), e); } Window.Current.Activate(); }
The OnActivated Event Handler will be triggered when Cortana Launches the application which is ActivationKind.VoiceCommand and the Command Property of the Library Class will be set to the result of this Voice input based on the VCD.xml.
Step 14
In the Solution Explorer select MainPage.xaml
Step 15
From the Menu choose View and then Designer
Step 16
The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:
<Grid Margin="50"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <TextBlock Grid.Row="0" x:Name="Title"/> <Rectangle Grid.Row="1" Name="Display"/> </Grid>
The block of XAML contains a Grid with two rows, the first is for a TextBoock and the second row is for a Rectangle.
Step 17
From the Menu choose View and then Code
Step 18
Once in the Code View, below the end of public MainPage() { … } the following Code should be entered:
protected override void OnNavigatedTo(NavigationEventArgs e) { Library.Parse(Title, Display); }
Below the MainPage() Method there is a OnNavigatedTo Event Handler and will call the Parse Method of the Library Class and will pass through the TextBlock and Rectangle Controls as Parameters.
Step 19
That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application
Step 20
One the Application has started running you need to then Exit the application by selecting the Close button in the top right of the Application
Step 21
After the Application has stopped running you can then tap the Microphone button from Cortana on the Windows 10 Taskbar or if enabled say Hey, Cortana then say “Colour Show Cornflower Blue” to show the spoken Colour in the Application
Step 22
To Exit the Application select the Close button in the top right of the Application