Windows 10 – Universal Windows Platform – Cortana

Step 1

Download Visual Studio Community 2015 and install it onto your computer, if it’s already downloaded and installed select Launch to start Visual Studio Community 2015 or if it has already been downloaded and installed then start the application you may also need to Enable your device for development.

2015-visual-studio

Step 2

Once Visual Studio Community 2015 has started select File, then New, then Project… from the Menu.

2015-file-new-project

Step 3

From the New Project window select Visual C# from Installed, Templates then select Blank App (Windows Universal) from the list, then type in a Name and select a Location to save to before selecting Ok to create the Project.

2015-new-project

Step 4

Once done select from the Menu, Project, then Add New Item…

2015-project-add-new-item

Step 5

From the Add New Item window select Visual C# from Installed and select Code 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

2015-add-new-item-library

Step 6

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.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 Windows.ApplicationModel.VoiceCommands.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);
            }
        }
    }
}

It should then appear as such:

2015-cortana-library

Step 7

From the Solution Explorer select App.xaml

2015-app-library

Step 8

Select from the Menu, View then Code

2015-view-code

Step 9

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();
}

It should then appear as such:

2015-cortana-app-code

Step 10

From the Solution Explorer select MainPage.xaml

2015-mainpage-library

Step 11

Select from the Menu, View then Designer

2015-view-designer

Step 12

The Design View will be displayed along with the XAML View and in this below <Grid Background=”{ThemeResource ApplicationPageBackgroundThemeBrush}”> enter the following XAML:

<Grid>
	<Grid.RowDefinitions>
		<RowDefinition Height="Auto"/>
		<RowDefinition Height="*"/>
	</Grid.RowDefinitions>
	<TextBlock Grid.Row="0" x:Name="Title"/>
	<Rectangle Grid.Row="1" Name="Display"/>
</Grid>

It should appear as such:

2015-cortana-mainpage-xaml

Step 13

Select from the Menu, View then Code

2015-view-code

Step 14

Once in the Code View below the public MainPage() { … } the following should be entered:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
	Library.Parse(Title, Display);
}

It should then appear as such:

2015-cortana-mainpage-code

Step 15

Once done select from the Menu, Project, then Add New Item…

2015-project-add-new-item

Step 16

From the Add New Item window select Visual C# from Installed and select Data 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

2015-cortana-add-new-xml

Step 17

Once in the View for VCD.xml below <?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>

It should then appear as such:

2015-cortana-vcd

Step 18

That completes the Universal Windows Application so Save the Project then select the Debug and Local Machine option to run the Application

2015-local-machine

Step 19

Once the Local Machine has started the Application should then appear

2015-cortana-local-machine-run

Step 20

Then Exit the application by selecting Stop Debugging in Visual Studio Community 2015

2015-stop

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

2015-cortana-local-machine-ran

Step 22

To Exit the application select the Close button on the top right of the Application

2015-close

Step 23

Another option is to run as a Windows 10 Mobile application, select Debug and select Emulator 10.0.10586.0 WVGA 4 inch 512MB option to run the Application

2015-emulator

Step 24

Once the Emulator has started the Application should then appear

2015-cortana-emulator-run

Step 25

Then Exit the application by selecting Stop Debugging in Visual Studio Community 2015

2015-stop

Step 26

After the Application has stopped running you can then tap the Search button on the bottom of the Emulator to launch Cortana you may need to set it up, so follow any steps to Use Cortana with a Microsoft Account but can choose to Sign in to just this app instead option when reached, once done can tap the Microphone button and then say “Colour Show Cornflower Blue” to launch the Application and show the spoken Colour

2015-compression-app-emulator-ran

Step 27

To Exit the application select the Close button on the top right of the Application

2015-close

Creative Commons License

Advertisements

One thought on “Windows 10 – Universal Windows Platform – Cortana

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s