C# 未为SaveCommandArgs执行ICommandHandler(Visual Studio for Mac扩展)
我正在尝试为Visual Studio for Mac创建简单的扩展,它将处理用户保存文档的时刻(示例项目位于GitHub上,) 下面是我的C# 未为SaveCommandArgs执行ICommandHandler(Visual Studio for Mac扩展),c#,visual-studio-extensions,visual-studio-mac,C#,Visual Studio Extensions,Visual Studio Mac,我正在尝试为Visual Studio for Mac创建简单的扩展,它将处理用户保存文档的时刻(示例项目位于GitHub上,) 下面是我的ICommandHandler实现的外观: [导出(类型化(ICommandHandler))] [姓名(姓名(SaveCommandHandler))] [ContentType(StandardContentTypeNames.Code)] [TextViewRole(预定义的TextViewRoles.PrimaryDocument)] 公共类Save
ICommandHandler
实现的外观:
[导出(类型化(ICommandHandler))]
[姓名(姓名(SaveCommandHandler))]
[ContentType(StandardContentTypeNames.Code)]
[TextViewRole(预定义的TextViewRoles.PrimaryDocument)]
公共类SaveCommandHandler:ICommandHandler
{
公共字符串DisplayName=>nameof(SaveCommandHandler);
私有只读IEditorCommandHandlerServiceFactory\u editorCommandHandlerServiceFactory;
[导入构造函数]
公共存储命令处理程序(IEditorCommandHandlerServiceFactory编辑器CommandHandlerServiceFactory)
{
_editorCommandHandlerServiceFactory=editorCommandHandlerServiceFactory;
}
public bool ExecuteCommand(SaveCommandArgs args,CommandExecutionContext executionContext)
{
尝试
{
var service=\u editorCommandHandlerServiceFactory.GetService(args.TextView);
WriteLine($“我正在使用{service.GetType()}在save上执行某些操作”);
}
捕获(例外情况除外)
{
Debug.WriteLine(ex);
}
返回true;
}
public CommandState GetCommandState(SaveCommandArgs args)
{
返回CommandState.Available;
}
}
好的方面:开始编辑第一个文件时,系统调用此处理程序的构造函数
缺点:ExecuteCommand
方法从未调用,以及GetCommandState
方法
你知道为什么它不起作用吗
我试着根据官方文件做所有的事情
不幸的是,只有快速信息和自动完成功能的示例。没有针对
ICommandHandler
-s的示例,也没有为Visual Studio for Mac找到任何类似的项目当您按SAVE时,它应该是这样的
[Export(typeof(ICommandHandler))]
[Name(nameof(SaveCommandHandler))]
[ContentType(StandardContentTypeNames.Code)]
[TextViewRole(PredefinedTextViewRoles.PrimaryDocument)]
public class SaveCommandHandler : ICommandHandler<SaveCommandArgs>
{
public string DisplayName => nameof(SaveCommandHandler);
private readonly IEditorCommandHandlerServiceFactory _editorCommandHandlerServiceFactory;
[ImportingConstructor]
public SaveCommandHandler(IEditorCommandHandlerServiceFactory editorCommandHandlerServiceFactory)
{
_editorCommandHandlerServiceFactory = editorCommandHandlerServiceFactory;
}
public bool ExecuteCommand(SaveCommandArgs args, CommandExecutionContext executionContext)
{
try
{
var service = _editorCommandHandlerServiceFactory.GetService(args.TextView);
Debug.WriteLine($"I am executing something on save with {service.GetType()}");
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
return true;
}
public CommandState GetCommandState(SaveCommandArgs args)
{
return CommandState.Available;
}
}