2012-01-02 89 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欄目。

相關問題