简体   繁体   English

Visual Studio:如何使用WPF编写编辑器扩展

[英]Visual Studio: How to write Editor Extensions with WPF

Im trying to write a editor extension for Visual Studio. 我正在尝试为Visual Studio编写编辑器扩展。 I have the downloaded VS SDK and created a new Visual Studio Package project. 我已经下载了VS SDK,并创建了一个新的Visual Studio Package项目。 But the dummy control created for me is a Windows Forms control and not a WPF control. 但是为我创建的虚拟控件是Windows Forms控件,而不是WPF控件。 I'm trying to replace it with a WPF-control but not doing so well. 我正在尝试将其替换为WPF控件,但效果不佳。 Is this possible anyhow? 这有可能吗?

Another related question: Is it only possible to write text-editors? 另一个相关的问题:是否只能编写文本编辑器? What I really want is a editor that looks more like a form with many different fields. 我真正想要的是一个看起来更像具有许多不同字段的表单的编辑器。 But it doesn't seem to be what this is made for? 但这似乎不是它的目的吗? There are a lot of interfaces on the EditorPane that imply a text-editor model only. EditorPane上有很多接口,仅暗示一个文本编辑器模型。

Ideally I want a editor much like the resx-editor where the file being edited has xml-content and the editor-ui is not a single textbox and where a generated cs-file is being outputted as a sub-file. 理想情况下,我想要一个类似于resx-editor的编辑器,其中要编辑的文件具有xml内容,并且editor-ui不是单个文本框,并且生成的cs文件将作为子文件输出。 Is this possible to do with editor extensions? 这可能与编辑器扩展有关吗?

This is explained in detail here: WPF in Visual Studio 2010 – Part 4 : Direct Hosting of WPF content 此处详细说明: Visual Studio 2010中的WPF –第4部分:WPF内容的直接托管

So, if you use the standard Extensibility / Custom Editor sample that comes with the Visual Studio SDK, what you can do to test it is this: 因此,如果您使用Visual Studio SDK随附的标准可扩展性/自定义编辑器示例,则可以对此进行测试:

1) Modify the provided EditorFactory.cs file like this: 1)修改提供的EditorFactory.cs文件,如下所示:

        // Create the Document (editor)
        //EditorPane NewEditor = new EditorPane(editorPackage); // comment this line
        WpfEditorPane NewEditor = new WpfEditorPane(); // add this line

2) create for example a WpfEditorPane.cs file like this: 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
}

Of course, you will have to implement all the editor logic (add interfaces, etc.) to mimic what's done in the Winforms sample, as what I provide here is really the minimal stuff for pure demonstration purposes. 当然,您将必须实现所有编辑器逻辑(添加接口等)以模仿Winforms示例中所做的事情,因为我在这里提供的内容实际上只是出于纯粹演示目的的最少内容。

NOTE: this whole "Content" thing only works starting with Visual Studio 2010 (so you need to make sure your project references Visual Studio 2010 assemblies, which should be the case if you start a project from scratch using Visual Studio 2010). 注意:整个“内容”仅适用于Visual Studio 2010(因此,您需要确保您的项目引用Visual Studio 2010程序集,如果您使用Visual Studio 2010从头开始项目,则应为这种情况)。 Hosting WPF editors within Visual Studio 2008 is possible using System.Windows.Forms.Integration.ElementHost . 可以使用System.Windows.Forms.Integration.ElementHost在Visual Studio 2008中托管WPF编辑器。

I'm not sure if the above is possible but a quick search did turn up this: http://karlshifflett.wordpress.com/2010/03/21/visual-studio-2010-xaml-editor-intellisense-presenter-extension/ 我不确定上述方法是否可行,但通过快速搜索确实发现了这一点: http : //karlshifflett.wordpress.com/2010/03/21/visual-studio-2010-xaml-editor-intellisense-presenter-extension /

and this: 和这个:

http://blogs.msdn.com/b/visualstudio/archive/2009/12/09/building-and-publishing-an-extension-for-visual-studio-2010.aspx http://blogs.msdn.com/b/visualstudio/archive/2009/12/09/building-and-publishing-an-extension-for-visual-studio-2010.aspx

Failing that, could you not host a WPF control inside the winforms control using ElementHost? 失败了,您是否不能使用ElementHost在winforms控件中托管WPF控件? I know its a lame workaround but might allow you to develop in your favourite toolkit while you find a more permanent solution. 我知道这是一个la脚的解决方法,但是当您找到更永久的解决方案时,可能会允许您使用自己喜欢的工具包进行开发。

Best regards, 最好的祝福,

There is a sample VS Extension Package application source code on MSDN: Designer View Over XML Editor . MSDN上有一个示例VS Extension Package应用程序源代码: Designer View over XML Editor

Description from site: 网站说明:
This Sample demonstrates how to create an extension with a WPF-based Visual Designer for editing XML files with a specific schema (XSD) in coordination with the Visual Studio XML Editor.

Admittedly solution is dedicated for VS2010 Extension Package, however it can be simply converted and attuned to VS2012 format - Target platform should be change to .NET 4.5, thereafter some references to VS2012 assemblies (v.11.0) should be added to build and run startup project: 公认的解决方案专用于VS2010扩展程序包,但是可以简单地将其转换并调整为VS2012格式-目标平台应更改为.NET 4.5,此后应添加一些对VS2012程序集(v.11.0)的引用以构建和运行启动项目:

Microsoft.VisualStudio.Shell.11.0.dll Microsoft.VisualStudio.Shell.11.0.dll
Microsoft.VisualStudio.Shell.Immutable.11.0.dll Microsoft.VisualStudio.Shell.Immutable.11.0.dll
Microsoft.VisualStudio.Shell.Interop.11.0 Microsoft.VisualStudio.Shell.Interop.11.0

In case of problems, please visit Q & A section of the site. 如有问题,请访问网站的“ Q & A部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM