I think I found a solution that works for me but still it would be nice to find a more elegant one if exists:
I define a dictation grammar and a "placeholder". Then I load my grammars and disabled them immediately.
using System.Speech.Recognition;
...
private DictationGrammar dictationGrammar;
private Grammar placeholderGrammar;
private List<Grammar> commands;
public void Initialize()
{
dictationGrammar = new DictationGrammar();
recognizer.LoadGrammarAsync(dictationGrammar);
var builder = new GrammarBuilder();
builder.Append("MYPLACEHOLDER");
placeholderGrammar = new Grammar(builder);
recognizer.LoadGrammarAsync(placeholderGrammar);
commands = new List<Grammar>();
foreach (var grammar in grammarManager.GetGrammars())
{
commands.Add(grammar);
grammar.Enabled = false;
recognizer.LoadGrammarAsync(grammar);
}
}
Then on the speechRecognized event I put the logic that if placeholder is recognized then enable the commands. If a command is recognized the re-enable the dictation and disable all commands:
private async void speechRecognized(object sender, SpeechRecognizedEventArgs e)
{
if (e.Result.Grammar == placeholderGrammar)
{
//go to command mode
placeholderGrammar.Enabled = false;
dictationGrammar.Enabled = false;
foreach (var item in commands)
item.Enabled = true;
}
else if (commands.Any(x => e.Result.Grammar == x))
{
Do_something_with_recognized_command("!!");
//go back in normal mode
placeholderGrammar.Enabled = true;
dictationGrammar.Enabled = true;
}else {//this is dictation.. nothing to do}
}