簡體   English   中英

Visual Studio:如何使用WPF編寫編輯器擴展

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

我正在嘗試為Visual Studio編寫編輯器擴展。 我已經下載了VS SDK,並創建了一個新的Visual Studio Package項目。 但是為我創建的虛擬控件是Windows Forms控件,而不是WPF控件。 我正在嘗試將其替換為WPF控件,但效果不佳。 這有可能嗎?

另一個相關的問題:是否只能編寫文本編輯器? 我真正想要的是一個看起來更像具有許多不同字段的表單的編輯器。 但這似乎不是它的目的嗎? EditorPane上有很多接口,僅暗示一個文本編輯器模型。

理想情況下,我想要一個類似於resx-editor的編輯器,其中要編輯的文件具有xml內容,並且editor-ui不是單個文本框,並且生成的cs文件將作為子文件輸出。 這可能與編輯器擴展有關嗎?

此處詳細說明: Visual Studio 2010中的WPF –第4部分:WPF內容的直接托管

因此,如果您使用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)例如創建一個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編輯器。

我不確定上述方法是否可行,但通過快速搜索確實發現了這一點: 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

失敗了,您是否不能使用ElementHost在winforms控件中托管WPF控件? 我知道這是一個la腳的解決方法,但是當您找到更永久的解決方案時,可能會允許您使用自己喜歡的工具包進行開發。

最好的祝福,

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部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM