最近我更新了 Visual Studio 并开始使用扩展宏资源管理器。
我尝试使用示例宏之一“删除并排序所有”,但我意识到如果我有一个打开的文档,它就不会运行。因此,我关闭了所有打开的文档,然后再次尝试,这次它打开所有文档并关闭它们,但它也不起作用。
问题是在文档完全加载之前执行的命令。如果有一个事件或计时器可以帮助等待文档完全加载,问题就会解决。
这是我的代码。我标记了要添加等待功能的位置:
function formatFile(file) {
dte.ExecuteCommand("View.SolutionExplorer");
if (file.Name.indexOf(".cs", file.Name.length - ".cs".length) !== -1) {
file.Open();
file.Document.Activate();
//here i want to wait for 1 second
dte.ExecuteCommand("Edit.RemoveAndSort");
file.Document.Save();
file.Document.Close();
}
}
我感谢任何形式的帮助。
GitHub 上的宏资源管理器:https://github.com/Microsoft/VS-Macros
最佳答案
根据您在原始帖子中共享的代码片段以及我还从 GitHub 克隆的项目,您的代码应该是 JavaScript 代码。
在 JavaScript 代码中,有 setTimeout() 或 setInterval() 函数。例如,如果您使用setTimeout(),则需要将暂停后需要运行的代码移至setTimeout()回调中。
请按如下方式修改您的代码。
function formatFile(file) {
dte.ExecuteCommand("View.SolutionExplorer");
if (file.Name.indexOf(".cs", file.Name.length - ".cs".length) !== -1) {
file.Open();
file.Document.Activate();
setTimeout(function () {
//move the code that you want to run after 1 second
dte.ExecuteCommand("Edit.RemoveAndSort");
file.Document.Save();
file.Document.Close();
}, 1000);
}
Javascript 代码中有很多关于 sleep() 的线程。请引用:
关于visual-studio - 如何在 Visual Studio 宏中给出时间延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46189416/