gpt4 book ai didi

wpf - Visual Studio : How to write Editor Extensions with WPF

转载 作者:行者123 更新时间:2023-12-04 12:58:56 25 4
gpt4 key购买 nike

我正在尝试为 Visual Studio 编写编辑器扩展。我已经下载了 VS SDK 并创建了一个新的 Visual Studio 包项目。但是为我创建的虚拟控件是 Windows 窗体控件而不是 WPF 控件。我正在尝试用 WPF 控件替换它,但效果不佳。这有可能吗?

另一个相关问题:是否只能编写文本编辑器?我真正想要的是一个看起来更像具有许多不同字段的表单的编辑器。但这似乎不是为了什么? EditorPane 上有很多接口(interface)仅暗示文本编辑器模型。

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

最佳答案

这里有详细解释: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) 例如创建一个 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
}

当然,您必须实现所有编辑器逻辑(添加接口(interface)等)以模仿 Winforms 示例中所做的工作,因为我在这里提供的内容实际上是用于纯粹演示目的的最少内容。

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

关于wpf - Visual Studio : How to write Editor Extensions with WPF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18761221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com