tgjones / gemini

Gemini is an IDE framework similar in concept to the Visual Studio Shell. It uses AvalonDock and has an MVVM architecture based on Caliburn Micro.
Other
1.1k stars 299 forks source link

How i can send a command from code. #273

Open gerry100 opened 7 years ago

gerry100 commented 7 years ago

Hi

i search a solution to send a save command to a document.

Thanks

JobaDiniz commented 7 years ago

My workaround:

public static bool CallSaveCommand(this ICommandService service)
        {
            try
            {
                var command = service.GetCommand(Gemini.Modules.Shell.ToolBarDefinitions.SaveFileToolBarItem.CommandDefinition);
                var target = service.GetTargetableCommand(command);
                target.Execute(null);

                return true;
            }
            catch (Exception ex)
            {
                Logger.Error("Error saving", ex.Unwrap());
                System.Windows.MessageBox.Show(ex.Unwrap().Message, "WDG Automation", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            return false;
        }

I've created an extension method for ICommandService. However, I also had to change TargetableCommand.Execute to wait the task, like so:

public void Execute(object parameter)
        {
            var commandHandler = _commandRouter.GetCommandHandler(_command.CommandDefinition);
            if (commandHandler == null)
                return;

            commandHandler.Run(_command).Wait(); //NOTE: had to call .Wait() otherwise this method is async, which is fine, except when we call commandService.CallSaveCommand()
        }