我正在尝试为Visual Studio编写编辑器扩展。我已经下载了VS SDK,并创建了一个新的Visual Studio Package项目。但是为我创建的虚拟控件是Windows Forms控件,而不是WPF控件。我正在尝试将其替换为WPF控件,但效果不佳。这有可能吗?

另一个相关的问题:是否只能编写文本编辑器?我真正想要的是一个看起来更像具有许多不同字段的表单的编辑器。但这似乎不是它的目的吗? EditorPane上有很多接口(interface),仅暗示一个文本编辑器模型。

理想情况下,我想要一个类似于resx-editor的编辑器,其中要编辑的文件具有xml内容,并且editor-ui不是单个文本框,并且生成的cs文件将作为子文件输出。这可能与编辑器扩展有关吗?

最佳答案

此处详细说明:WPF in Visual Studio 2010 – Part 4 : Direct Hosting of WPF content

因此,如果您使用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

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
}

当然,您将必须实现所有编辑器逻辑(添加接口(interface)等)以模仿Winforms示例中的操作,因为我在此处提供的内容实际上只是出于纯粹演示目的的最少内容。

注意:整个“内容”仅适用于Visual Studio 2010(因此,您需要确保您的项目引用了Visual Studio 2010程序集,如果使用Visual Studio 2010从头开始项目,则应为这种情况)。可以使用System.Windows.Forms.Integration.ElementHost在Visual Studio 2008中托管WPF编辑器。

08-04 09:07