Пример #1
0
        private void RecListener_Recognized(object sender, string recognized)
        {
            isRecording = false;
            recognized  = recognized.ToLower();
            var displayCommand = AllRegisteredCommands["type"];

            displayCommand.ExecuteWithResult(recognized);
            if (AllRegisteredCommands.ContainsKey(recognized))
            {
                var command = AllRegisteredCommands[recognized];
                if (command.CanExecute())
                {
                    command.Execute();
                }
            }
            else
            {
                var result = Demo.demoMethod(recognized);
                System.Diagnostics.Debug.WriteLine(result);
            }


            MessagingCenter.Send <IVoiceToCommandService, string>(this, "STT", recognized);
        }
Пример #2
0
        public void RegisterCommand(MethodInfo method, bool ignoreInvalid)
        {
            CommandAttribute attribute;

            try
            {
                attribute = method.GetCustomAttribute <CommandAttribute>();
            }
            catch (System.IO.FileNotFoundException)
            {
                // If the method has an attribute which is part of an assembly that isn't loaded,
                // you'll get a FileNotFoundException here.
                return;
            }

            if (attribute == null)
            {
                if (ignoreInvalid)
                {
                    return;
                }
                else
                {
                    throw new InvalidCommandMethodException("Command methods must be decorated with the [Command] attribute");
                }
            }

            string commandName = String.IsNullOrEmpty(attribute.Name) ? method.Name : attribute.Name;

            if (commandName.StartsWith("$"))
            {
                throw new InvalidCommandMethodException("Command names cannot start with '$'");
            }


            var registryEntry = AllRegisteredCommands.GetEntry(commandName);
            var command       = new Command(commandName, attribute.Description, method, attribute.ProviderType, attribute.Hidden);

            if (
                registryEntry.Commands.Any(o
                                           => o.RequiredParamCount == command.RequiredParamCount &&
                                           o.OptionalParamCount == command.OptionalParamCount)
                )
            {
                if (ignoreInvalid)
                {
                    return;
                }
                else
                {
                    throw new InvalidCommandMethodException("That command name, with that number of parameters, is already in use");
                }
            }


            registryEntry.Commands.Add(command);

            // Also track commands by assembly, for nicer `help` methods
            string assemblyName = method.DeclaringType.Assembly.GetName().Name;

            if (!CommandsByAssembly.TryGetValue(assemblyName, out var assemblyCommandList))
            {
                assemblyCommandList = new List <Command>();
                CommandsByAssembly.Add(assemblyName, assemblyCommandList);
            }
            assemblyCommandList.Add(command);
        }