Page 53 - MSDN Magazine, April 2017
P. 53

Figure 5 Voice Commands Handler Code
information and then be sent to a remote Web API or service. The code to read the setting can usually be written in the App_activated event handler where the app is launching and the information per- sisted from the previous app session can be accessed. Note that the name of each setting has a 255-character limit and each setting has a limit of 8K bytes in size.
Voice (Cortana) Integration Scenario
One of the scenarios that your Web site (now an app) can implement— post porting to the UWP—is to integrate Cortana, a voice-based interactive digital assistant on Windows devices.
Consider that the Web site is a travel-based portal and now, ported to a Windows 10 app, one of the use cases where Cortana can be inte- grated is to show details of a person’s trip. The user can then issue commands such as, “Show trip to London” (or whatever destination), which needs to be configured by app developers, as shown in Figure 4.
Cortana can help achieve tasks with a lot of first-party apps (such as Calendar) and expose APIs to interface with custom apps. Basically, here’s basically how Cortana works with apps:
1. Listens to commands as registered in the voicecommands.xml file.
2. Activates the relevant app, matching the commands spoken/typed in the Windows Search bar.
3. Passes the speech to/text commands to app as variables to let the app process the input from user.
Note: The file name is indicative. It can be named as desired with an XML extension. The Windows Developer article, “Using CortanatoInteractwithYourCustomers(10by10),”atbit.ly/2iGymdE provides a good overview of configuring commands in the XML file and its schema.
The XML code in Figure 4 will help Cortana identify that the Adventure Works app needs to launch when issued commands such as the following:
'Adventure Works, Show trip to London' 'Adventure Works, Show trip to Dallas'
Now, let’s take a look at how the Web code would handle the activation and navigation to the relevant page within the app (or
<!-- In HTML page :
<meta name="msapplication-cortanavcd" content="http://<URL>/vcd.xml" /> -->
(function () {
if (typeof Windows !== 'undefined' &&
typeof Windows.UI !== 'undefined' &&
typeof Windows.ApplicationModel !== 'undefined') {
Windows.UI.WebUI.WebUIApplication.addEventListener("activated", activatedEvent); }
function activatedEvent (args) {
var activation = Windows.ApplicationModel.Activation;
// Check to see if the app was activated by a voice command. if (args.kind === activation.ActivationKind.voiceCommand) {
// Get the speech recognition.
var speechRecognitionResult = args.result;
var textSpoken = speechRecognitionResult.text;
// Determine the command type {search} defined in vcd. switch (textSpoken) {
case "London": window.location.href =
'https://<mywebsite.webapp.net>/Pages/Cities.html?value=London'; break;
case "Dallas": window.location.href =
'https://<mywebsite.webapp.net>/Pages/Cities.html?value=Dallas'; break;
... <other cases> ...
}
} }
})();
Consider a scenario in which certain parameters need to be per- sisted between sessions by the same user. For example, consider a string identifying the user and other miscellaneous information such as last session time and other cached items.
The right class to use in this scenario is the Windows.Storage.Ap- plicationData class. This class has many properties and methods, however, consider the localSettings property for this scenario.
The settings are stored in a key-value pair system. Look at the following sample code for the implementation:
function getSessionData()
{
var applicationData = Windows.Storage.ApplicationData.current; var localSettings = applicationData.localSettings;
// Create a simple setting. localSettings.values["userID"] = "user998-i889-27";
// Read data from a simple setting.
var value = localSettings.values["userID"];
}
Ideally, the code to write data into the settings can be written at checkpoints within the app where it’s necessary to capture certain msdnmagazine.com
Figure 6 Back-Button Code
(function() {
if (typeof Windows !== 'undefined' &&
typeof Windows.UI !== 'undefined' &&
typeof Windows.ApplicationModel !== 'undefined') {
Windows.UI.Core.SystemNavigationManager.getForCurrentView(). appViewBackButtonVisibility = Windows.UI.Core.AppViewBackButtonVisibility.visible;
Windows.UI.Core.SystemNavigationManager.getForCurrentView(). addEventListener("backrequested", onBackRequested);
function onBackRequested(eventArgs) {
window.history.back();
eventArgs.handled = true; }
} })();
April 2017 39









































   51   52   53   54   55