Visual Studio:如何使用WPF编写Editor Extensions

And*_*ita 11 wpf plugins add-in visual-studio

我试图为Visual Studio编写一个编辑器扩展.我有下载的VS SDK并创建了一个新的Visual Studio Package项目.但为我创建的虚拟控件是Windows窗体控件而不是WPF控件.我试图用WPF控件取而代之,但效果不佳.无论如何这可能吗?

另一个相关问题:是否只能编写文本编辑器?我真正想要的是一个看起来更像是具有许多不同领域的表单的编辑器.但它似乎不是这样做的?EditorPane上有许多接口仅暗示文本编辑器模型.

理想情况下,我想要一个与resx-editor非常相似的编辑器,其中正在编辑的文件具有xml-content,而editor-ui不是单个文本框,并且生成的cs文件作为子文件输出.这可能与编辑器扩展有关吗?

Sim*_*ier 7

这在此详细解释:Visual Studio 2010中的WPF - 第4部分:WPF内容的直接托管

因此,如果您使用Visual Studio SDK附带的标准扩展性/自定义编辑器示例,您可以执行以下操作来测试它:

1)修改提供的EditorFactory.cs文件,如下所示:

        // Create the Document (editor)
        //EditorPane NewEditor = new EditorPane(editorPackage); // comment this line
        WpfEditorPane NewEditor = new WpfEditorPane(); // add this line
Run Code Online (Sandbox Code Playgroud)

2)创建一个WpfEditorPane.cs这样的文件:

[ComVisible(true)]
public class WpfEditorPane : WindowPane, IVsPersistDocData
{
    private TextBox _text;

    public WpfEditorPane()
        : base(null)
    {
        _text = new TextBox(); // Note this is the standard WPF thingy, not the Winforms one
        _text.Text = "hello world";
        Content = _text; // use any FrameworkElement-based class here.
    }

    #region IVsPersistDocData Members
    // NOTE: these need to be implemented properly! following is just a sample

    public int Close()
    {
        return VSConstants.S_OK;
    }

    public int GetGuidEditorType(out Guid pClassID)
    {
        pClassID = Guid.Empty;
        return VSConstants.S_OK;
    }

    public int IsDocDataDirty(out int pfDirty)
    {
        pfDirty = 0;
        return VSConstants.S_OK;
    }

    public int IsDocDataReloadable(out int pfReloadable)
    {
        pfReloadable = 0;
        return VSConstants.S_OK;
    }

    public int LoadDocData(string pszMkDocument)
    {
        return VSConstants.S_OK;
    }

    public int OnRegisterDocData(uint docCookie, IVsHierarchy pHierNew, uint itemidNew)
    {
        return VSConstants.S_OK;
    }

    public int ReloadDocData(uint grfFlags)
    {
        return VSConstants.S_OK;
    }

    public int RenameDocData(uint grfAttribs, IVsHierarchy pHierNew, uint itemidNew, string pszMkDocumentNew)
    {
        return VSConstants.S_OK;
    }

    public int SaveDocData(VSSAVEFLAGS dwSave, out string pbstrMkDocumentNew, out int pfSaveCanceled)
    {
        pbstrMkDocumentNew = null;
        pfSaveCanceled = 0;
        return VSConstants.S_OK;
    }

    public int SetUntitledDocPath(string pszDocDataPath)
    {
        return VSConstants.S_OK;
    }

    #endregion
}
Run Code Online (Sandbox Code Playgroud)

当然,您必须实现所有编辑器逻辑(添加接口等)以模仿Winforms示例中的操作,因为我在这里提供的内容实际上是用于纯粹演示目的的最小内容.

注意:整个"内容"事物仅适用于Visual Studio 2010(因此您需要确保您的项目引用Visual Studio 2010程序集,如果您使用Visual Studio 2010从头开始创建项目,则应该如此).使用System.Windows.Forms.Integration.ElementHost可以在Visual Studio 2008中托管WPF编辑器.