Skip to content

Instantly share code, notes, and snippets.

@LanceMcCarthy
Created May 11, 2016 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LanceMcCarthy/2eaa9adcff2b1dd2dc5812c47f19803b to your computer and use it in GitHub Desktop.
Save LanceMcCarthy/2eaa9adcff2b1dd2dc5812c47f19803b to your computer and use it in GitHub Desktop.
VCD helper
public static class VoiceCommandUtilites
{
private static int loadAttempts;
public static async Task InstallVoiceCommandFile()
{
if (loadAttempts > 0)
{
Debug.WriteLine($"-------Voice Commands load attempt max reached. loadAttempt: {loadAttempts}--------");
return;
}
var alreadyLoaded = HaveVoiceCommandsBeenLoaded();
Debug.WriteLine($"-------HaveVoiceCommandsBeenLoaded() {alreadyLoaded}--------");
if (alreadyLoaded)
return;
try
{
StorageFile vcdFile = null;
try
{
//vcdFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///VoiceCommands.xml"));
vcdFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///VoiceCommandsRedstone.xml"));
//vcdFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///VoiceCommandsTest.xml"));
if (vcdFile == null)
{
Debug.WriteLine($"-------vcdFile was null, returning--------");
ApplicationData.Current.LocalSettings.Values["VoiceCommandsLoaded"] = false;
return;
}
Debug.WriteLine($"-------VoiceCommands.xml is present--------");
}
catch (FileNotFoundException ex)
{
Debug.WriteLine($"-------VoiceCommands.xml FileNotFound Exception-------- \r\nError: {ex.Message}");
ApplicationData.Current.LocalSettings.Values["VoiceCommandsLoaded"] = false;
return;
}
catch (Exception ex)
{
Debug.WriteLine($"-------VoiceCommandUtilities Exception--------" +
$"\r\nStorageFile.GetFileFromApplicationUriAsync " +
$"\r\nError: {ex.Message}");
ApplicationData.Current.LocalSettings.Values["VoiceCommandsLoaded"] = false;
return;
}
try
{
await VoiceCommandDefinitionManager.InstallCommandDefinitionsFromStorageFileAsync(vcdFile);
Debug.WriteLine($"-------VoiceCommands loaded--------");
ApplicationData.Current.LocalSettings.Values["VoiceCommandsLoaded"] = true;
}
catch (Exception ex)
{
Debug.WriteLine($"-------VoiceCommandDefinitionManager.InstallCommandSetsFromStorageFileAsync() Exception--------\r\n-------Exception Message: {ex.Message}");
}
}
catch (Exception ex)
{
Debug.WriteLine($"-------VoiceCommandUtilites.InstallVoiceCommandFile() Exception--------{ex}");
ApplicationData.Current.LocalSettings.Values["VoiceCommandsLoaded"] = false;
}
finally
{
loadAttempts++;
}
}
private static bool HaveVoiceCommandsBeenLoaded()
{
try
{
object val;
if (ApplicationData.Current.LocalSettings.Values.TryGetValue("VoiceCommandsLoaded", out val))
{
return (bool) val;
}
}
catch (Exception ex)
{
Debug.WriteLine($"------HaveVoiceCommandsBeenLoaded() Exception-------\r\n{ex}");
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment