此处详细解释: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 从头开始启动项目,则应该是这种情况)。可以使用以下方式在 Visual Studio 2008 中托管 WPF 编辑器System.Windows.Forms.Integration.ElementHost.