我的主要目标是对当前XAML进行简单的可视反馈;我还没有达到需要设计师支持可视化编辑的阶段。如果有人感兴趣,这是一个模拟工业机械的工具;我使用XAML来定义机器的组件及其连接,但目前必须运行完整的模拟才能看到机器的外观。
你要找的是ProvideXmlEditorChooserDesignerViewAttribute,虽然你必须以这种方式使用它才能使其正常工作,但它有点奇怪。 https://msdn.microsoft.com/zh-cn/library/microsoft.visualstudio.modeling.shell.providexmleditorchooserdesignerviewattribute_properties.aspx
我刚刚在自己的扩展中让它工作了,而且非常容易(一旦你掌握了魔法公式)。从VS Extensibility示例“带工具箱的编辑器”(https://github.com/Microsoft/VSSDK-Extensibility-Samples/tree/master/Editor_With_Toolbox)开始,按照以下步骤进行:
打开EditorPackage.cs并删除ProvideEditorExtension和ProvideEditorLogicalView属性。用这些属性替换它们(显然用实际字符串替换存根字符串)。
[ProvideXmlEditorChooserDesignerView("UnimportantDesignerViewName", "xaml",
LogicalViewID.Designer, 10000,
DesignerLogicalViewEditor = typeof(EditorFactory),
Namespace = "YourNamespace",
MatchExtensionAndNamespace = true)]
[ProvideXmlEditorChooserDesignerView("UnimportantDesignerViewName_Enforced", "xaml",
LogicalViewID.Designer, 10001,
DesignerLogicalViewEditor = typeof(EditorFactory))]
[ProvideEditorLogicalView(typeof(EditorFactory), LogicalViewID.TextView)]
[ProvideEditorLogicalView(typeof(EditorFactory), LogicalViewID.Code)]
[ProvideEditorLogicalView(typeof(EditorFactory), LogicalViewID.Designer)]
[ProvideEditorLogicalView(typeof(EditorFactory), LogicalViewID.Debugging)]
打开EditorFactory.cs并编辑CreateEditorInstance方法。在这里,您将打开由pszMkDocument文件路径参数指定的.xaml文件,并验证它是否属于您的文件(例如,它使用您的模式或其他内容)。如果是,则输出您的EditorFactory Guids并返回S_OK。如果不是(例如,如果它是WPF.xaml文件),则输出Guid.Empty并返回VS_E_UNSUPPORTEDFORMAT。
string extension = System.IO.Path.GetExtension(pszMkDocument);
if (extension.Equals(".xaml", StringComparison.OrdinalIgnoreCase))
{
using (System.Xml.XmlReader reader = System.Xml.XmlReader.Create(pszMkDocument))
{
reader.MoveToContent();
if (reader.NodeType == System.Xml.XmlNodeType.Element)
{
if (reader.NamespaceURI.Equals("YourNamespace", StringComparison.OrdinalIgnoreCase))
{
EditorPane newEditor = new EditorPane();
ppunkDocView = Marshal.GetIUnknownForObject(newEditor);
ppunkDocData = Marshal.GetIUnknownForObject(newEditor);
pbstrEditorCaption = "";
return VSConstants.S_OK;
}
}
}
}
return VSConstants.VS_E_UNSUPPORTEDFORMAT;
我是通过 Avalonia GitHub 代码库 (https://github.com/AvaloniaUI/AvaloniaVS/blob/master/src/AvaloniaVS/Infrastructure/AvaloniaPackage.cs) 偶然发现了这个神奇的属性公式,非常感谢那些人为解决这个问题而付出的努力。我在 VS 2017 Community 中实现了它,对我来说工作得非常好。祝所有试图完成同样神秘任务的人好运。=)