2012-01-02 90 views
10

我试图编写Visual Studio的编辑器扩展。我有下载的VS SDK并创建了一个新的Visual Studio包项目。但为我创建的虚拟控件是一个Windows窗体控件,而不是WPF控件。我试图用WPF控件替换它,但是做得不好。无论如何,这可能吗?Visual Studio:如何使用WPF编写扩展编辑器

另一个相关的问题:是否只能编写文本编辑器?我真正想要的是一个看起来更像是一个有许多不同领域的形式的编辑。但它似乎并不是为此而做的? EditorPane上有很多接口,仅暗示文本编辑器模型。

理想我希望有一个编辑器很像,其中正在编辑的文件具有XML的内容和编辑器的UI是不是一个单一的文本框,并在那里所产生的CS-文件被输出作为分档的RESX编辑器。这可能与编辑器扩展有关吗?

+0

在VS Extensions中,“编辑者”一般是文本/代码编辑器 - 也许你想创建一个“设计器”来代替。 – 2012-01-02 23:26:19

+0

相关问题:[链接](http://stackoverflow.com/questions/18761221/visual-studio-2012-wpf-custom-design er-editor) – sgnsajgon 2013-09-12 12:56:27

回答

7

中对此有详细说明如下: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)CRE吃例如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 
} 

当然,你将必须实现所有的编辑逻辑(添加接口等),以模仿有什么的WinForms样品中完成的,因为我在这里提供的真正用于纯演示目的的最低限度的东西。

注意:这整个“内容”的东西只适用于从Visual Studio 2010开始(所以你需要确保你的项目引用Visual Studio 2010程序集,如果你使用Visual Studio 2010从头开始一个项目应该是这种情况)。使用System.Windows.Forms.Integration.ElementHost可以在Visual Studio 2008中托管WPF编辑器。

+0

您的回答或类似问题也可能适用于此主题: [链接](http://stackoverflow.com/questions/3989322/can-i-create-a-visual-studio-2010-add-in-that-uses-a-wpf-display),你可以在哪里赚取额外的+50赏金。 – sgnsajgon 2013-09-16 10:47:34

+0

@sgnsajgon - 看起来像是我的重复。你应该删除对链接的赏金,所以我们可以把它标记为这个的复制品(如果是的话) – 2013-09-16 13:32:48

2

我不知道,如果上面是可能的,但快速搜索并打开了这一点: http://karlshifflett.wordpress.com/2010/03/21/visual-studio-2010-xaml-editor-intellisense-presenter-extension/

这:

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

如果做不到这一点,你能不能举办一个WPF使用ElementHost控制Winforms控件内部?我知道它是一种蹩脚的解决方法,但可能允许您在找到更持久的解决方案时开发自己喜欢的工具包。

最好的问候,

1

在MSDN上有一个示例VS Extension Package应用程序源代码:Designer View Over XML Editor。从现场

说明:
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.

不可否认的解决方案是专门为VS2010扩展包,但它可以简单地转换并切合VS2012格式 - 目标平台应该改变.NET 4.5,此后一些参考VS2012程序集(v.11。0)应该被添加到构建和运行启动项目:

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

如有问题,请访问网站的Q & A栏目。

相关问题