Visual Studio:如何使用WPF编写编辑器扩展 [英] Visual Studio: How to write Editor Extensions with WPF

查看:297
本文介绍了Visual Studio:如何使用WPF编写编辑器扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Visual Studio编写编辑器扩展.我已经下载了VS SDK,并创建了一个新的Visual Studio Package项目.但是为我创建的虚拟控件是Windows Forms控件,而不是WPF控件.我正在尝试将其替换为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?

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

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.

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

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?

推荐答案

此处详细说明:因此,如果您使用Visual Studio SDK随附的标准可扩展性/自定义编辑器示例,则可以对此进行测试:

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)像这样修改提供的EditorFactory.cs文件:

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)创建例如这样的WpfEditorPane.cs文件:

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
}

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

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.

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

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.

这篇关于Visual Studio:如何使用WPF编写编辑器扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆