Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio: How to write Editor Extensions with WPF

Im trying to write a editor extension for Visual Studio. I have the downloaded VS SDK and created a new Visual Studio Package project. But the dummy control created for me is a Windows Forms control and not a WPF control. I'm trying to replace it with a WPF-control but not doing so well. 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.

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. Is this possible to do with editor extensions?

like image 619
Andreas Zita Avatar asked Jan 02 '12 20:01

Andreas Zita


People also ask

How do I use Visual Studio code extensions in Visual Studio?

You can browse and install extensions from within VS Code. Bring up the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of VS Code or the View: Extensions command (Ctrl+Shift+X). This will show you a list of the most popular VS Code extensions on the VS Code Marketplace.

What language are VS Code extensions written in?

VS Code extensions support two main languages: JavaScript and TypeScript.


1 Answers

This is explained in detail here: WPF in Visual Studio 2010 – Part 4 : Direct Hosting of WPF content

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:

1) Modify the provided EditorFactory.cs file like this:

        // 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:

[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.

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). Hosting WPF editors within Visual Studio 2008 is possible using System.Windows.Forms.Integration.ElementHost.

like image 169
Simon Mourier Avatar answered Sep 28 '22 02:09

Simon Mourier