我正在尝试为 Visual Studio for Mac 开发扩展。我正在使用 this 教程。一切都很顺利,直到我尝试运行我的扩展程序。在我的情况下,“编辑”子菜单中的“插入日期”被禁用。在调试时,我注意到 IdeApp.Workbench.ActiveDocument.Editor 为空,尽管我有一个打开的文档。这是我的代码
using System;
using MonoDevelop.Components.Commands;
using MonoDevelop.Ide;
namespace ExampleIDEExtension
{
public class InsertDateHandler : CommandHandler
{
protected override void Run()
{
var editor = IdeApp.Workbench.ActiveDocument.Editor;
var currentTime = DateTime.Now.ToString();
editor.InsertAtCaret(currentTime);
}
protected override void Update(CommandInfo info)
{
info.Enabled = IdeApp.Workbench.ActiveDocument.Editor != null;
}
}
}
尽管有一个打开的文档,但我不知道为什么 Editor 为空。
最佳答案
编辑器为空,因为 Monodevelop 正在使用 Microsoft.VisualStudio.Text.Editor 并且它在下面的链接中提到 API 已过时。
https://github.com/mono/monodevelop/blob/50fbe0a7e65c5439e3313c6b50e7ef927f5f1fe9/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Editor/TextEditor.cs
无论如何,要回答您的问题,这就是我为实现插入日期处理程序演示插件而必须做的
protected override void Run()
{
var textBuffer = IdeApp.Workbench.ActiveDocument.GetContent<ITextBuffer>();
var date = DateTime.Now.ToString();
var textView = IdeApp.Workbench.ActiveDocument.GetContent<ITextView>();
var caretPosition = textView.Caret.Position;
textBuffer.Insert(caretPosition.BufferPosition.Position,date);
}
protected override void Update(CommandInfo info)
{
var textBuffer = IdeApp.Workbench.ActiveDocument.GetContent<ITextBuffer>();
if (textBuffer != null && textBuffer.AsTextContainer() is SourceTextContainer container)
{
var document = container.GetTextBuffer();
if (document != null)
{
info.Enabled = true;
}
}
}
关于c# - VS for Mac 扩展 - ActiveWindow 中的空编辑器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58048719/