简体   繁体   English

调试时VS2012的VSIX扩展未运行

[英]VSIX extension for VS2012 not running when debugging

I created a new VSIX extension project in Visual Studio 2012, and wrote a MEF classifier (as a test) that should simply highlight all text in a .mylang file. 我在Visual Studio 2012中创建了一个新的VSIX扩展项目,并编写了一个MEF分类器(作为测试),它应该只是突出显示.mylang文件中的所有文本。 Here are the relevant parts of my .NET 4.5 code: 以下是我的.NET 4.5代码的相关部分:

internal static class MyLangLanguage
{
    public const string ContentType = "mylang";

    public const string FileExtension = ".mylang";

    [Export(typeof(ClassificationTypeDefinition))]
    [Name(ContentType)]
    [BaseDefinition("code")]
    internal static ContentTypeDefinition MyLangSyntaxContentTypeDefinition = null;

    [Export]
    [FileExtension(FileExtension)]
    [ContentType(ContentType)]
    internal static FileExtensionToContentTypeDefinition MyLangSyntaxFileExtensionDefinition = null;
}

[Export(typeof(IClassifierProvider))]
[ContentType(MyLangLanguage.ContentType)]
[Name("MyLangSyntaxProvider")]
internal sealed class MyLangSyntaxProvider : IClassifierProvider
{
    [Import]
    internal IClassificationTypeRegistryService ClassificationRegistry = null;

    public IClassifier GetClassifier(ITextBuffer buffer)
    {
        return buffer.Properties.GetOrCreateSingletonProperty(() => new MyLangSyntax(ClassificationRegistry, buffer));
    }
}

internal sealed class MyLangSyntax : IClassifier { }

Here is the full code . 这是完整的代码

These are the relevant parts from my source.extension.vsixmanifest file. 这些是我的source.extension.vsixmanifest文件中的相关部分。 Based on suggestions and similar files I found across the web, I added the dependency on MPF and the two assets. 根据我在网上找到的建议和类似文件,我添加了对MPF和两个资产的依赖。

<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
    <!-- ... -->
    <Dependencies>
        <Dependency Id="Microsoft.Framework.NDP" DisplayName="Microsoft .NET Framework" d:Source="Manual" Version="4.5" />
        <Dependency d:Source="Installed" Id="Microsoft.VisualStudio.MPF.11.0" DisplayName="Visual Studio MPF 11.0" Version="[11.0,12.0)" />
    </Dependencies>
    <Assets>
        <Asset Type="Microsoft.VisualStudio.VsPackage" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%;PkgdefProjectOutputGroup|" />
        <Asset Type="Microsoft.VisualStudio.MefComponent" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%|" />
    </Assets>
</PackageManifest>

I also tried a version 1.0 manifest: 我还尝试了1.0版本清单:

<?xml version="1.0" encoding="utf-8"?>
<Vsix Version="1.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2010">
    <!-- ... -->
    <References />
    <Content>
        <MefComponent>|%CurrentProject%|</MefComponent>
    </Content>
</Vsix>

When I run it, it starts an experimental instance of Visual Studio 2012, and the Extensions and Updates window shows that my extension is active. 当我运行它时,它启动Visual Studio 2012的实验性实例,“ 扩展和更新”窗口显示我的扩展是活动的。 However, it does not do anything when I load or create a .mylang file. 但是,当我加载或创建.mylang文件时,它不会执行任何操作。 Any exceptions I throw (as a test) from my extension are never thrown. 我从我的扩展中抛出(作为测试)的任何异常都不会被抛出。 Breakpoints are never hit, and get an exclamation mark with the following warning: 断点永远不会被击中,并获得带有以下警告的感叹号:

The breakpoint will not currently be hit. 断点当前不会被击中。 No symbols have been loaded for this document. 没有为此文档加载任何符号。

It feels as if my extension is never really loaded at all. 感觉好像我的扩展从未真正加载过。 My problem is similar to this problem and this problem , but I'm using Visual Studio 2012 which uses a new VSIX manifest format. 我的问题类似于这个问题这个问题 ,但我使用的是使用新的VSIX清单格式的Visual Studio 2012。

What I know: 我知道的:

  • I can find my DLL and VSIX file in the %localappdata%\\Microsoft\\VisualStudio\\11.0Exp\\Extensions\\MyLang\\VSIXProject1\\1.0 folder, so I know they are copied. 我可以在%localappdata%\\Microsoft\\VisualStudio\\11.0Exp\\Extensions\\MyLang\\VSIXProject1\\1.0文件夹中找到我的DLL和VSIX文件,因此我知道它们已被复制。
  • Their timestamp corresponds to when I last built the project, so I know they are up-to-date. 他们的时间戳对应于我上次构建项目的时间,因此我知道它们是最新的。
  • Project Properties > Debug > Start external program: is already automatically set to C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\Common7\\IDE\\devenv.exe , and the Command line arguments were automatically set to /rootsuffix Exp . 项目属性>调试>启动外部程序:已自动设置为C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\Common7\\IDE\\devenv.exe ,并且命令行参数自动设置为/rootsuffix Exp
  • The Visual Studio log (created with the /log option) has two entries related to my extension: Successfully loaded extension... and Extension is enabled... . Visual Studio日志(使用/log选项创建)有两个与我的扩展相关的条目: Successfully loaded extension...Extension is enabled...
  • My DLL does not appear on the Modules tab (list of all loaded DLLs) of the debugging Visual Studio, while some (not all) other extensions do appear. 我的DLL 没有出现在调试Visual Studio的“ 模块”选项卡(所有已加载的DLL的列表)上,而某些(并非所有)其他扩展名确实出现。
  • It doesn't get loaded in Visual Studio 2012 or 2010 both on my laptop and my desktop PC. 它不会在我的笔记本电脑和台式机上加载到Visual Studio 2012或2010中。

What I've tried: 我尝试过的:

  • Set <IncludeAssemblyInVSIXContainer> to true in the .csproj file, per this suggestion , but it did not make any difference. 根据此建议 ,在.csproj文件中将<IncludeAssemblyInVSIXContainer>设置为true ,但它没有任何区别。
  • I can't add the line <MefComponent>|%CurrentProject%|</MefComponent> to the source.extension.vsixmanifest file as it uses a different format (2.0) than VSIX projects for previous versions of Visual Studio (1.0). 不能添加行<MefComponent>|%CurrentProject%|</MefComponent>因为它使用比VSIX项目的Visual Studio(1.0)的先前版本不同的格式(2.0)到source.extension.vsixmanifest文件。
  • This suggestion (setting IncludeAssemblyInVSIXContainer and friends in my .csproj to true ) but it does not make a difference. 这个建议 (将我的.csproj中的 IncludeAssemblyInVSIXContainer和朋友设置为true )但它没有什么区别。 And my breakpoints are still showing the warning and not being hit. 我的断点仍然显示警告而没有被击中。
  • Reset the VS Experimental instance using the Reset the Visual Studio 2012 Experimental Instance shortcut in the Start Menu, as per this suggestion . 根据此建议 ,使用“开始”菜单中的“ 重置Visual Studio 2012实验实例”快捷方式重置VS实验实例。 It didn't make a difference. 它没有任何区别。

How can I at the very least be sure my VSIX MEF extension is loaded and works? 我怎么能至少确定我的VSIX MEF扩展已加载并且有效? And if possible, how can I make by breakpoint work and debug it? 如果可能的话,我如何通过断点工作并调试它?

Edit: The problem is you've improperly exported your ContentTypeDefinition as a ClassificationTypeDefinition . 编辑:问题是您已将ContentTypeDefinition错误地导出为ClassificationTypeDefinition You should use the following instead: 您应该使用以下代码:

[Export] // <-- don't specify the type here
[Name(ContentType)]
[BaseDefinition("code")]
internal static ContentTypeDefinition MyLangSyntaxContentTypeDefinition = null;

Here's my two guesses right now: 这是我现在的两个猜测:

  1. Try removing the following line from your vsixmanifest. 尝试从vsixmanifest中删除以下行。 I assume you do not have a class in your project that extends Package , in which case Visual Studio might be refusing to load your package due to the following Asset line (your extension does not actually provide this asset). 我假设您的项目中没有扩展Package ,在这种情况下,由于以下资产行,Visual Studio可能拒绝加载您的包(您的扩展实际上并未提供此资产)。

     <Asset Type="Microsoft.VisualStudio.VsPackage" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%;PkgdefProjectOutputGroup|" /> 
  2. If that fails, try replacing your current source.extension.vsixmanifest with one written to the old schema (version 1.0). 如果失败,请尝试将当前的source.extension.vsixmanifest替换为写入旧架构(版本1.0)的源码。 I know this form still works in Visual Studio 2012 because all ~20 extensions I work on (with >10 public releases) use the old schema. 我知道这个表单在Visual Studio 2012中仍然有效,因为我所使用的所有~20个扩展(具有> 10个公共版本)都使用旧模式。

280Z28 solved the problem ! 280Z28解决了这个问题 For completeness, this is the full tried and tested code that will create a super simple VSIX Visual Studio MEF extension that colors all text in a .mylang file blue (or whatever the current keyword color is). 为了完整,这是完全经得起考验的代码,将创建一个超级简单的VSIX Visual Studio的MEF扩展,颜色的所有文字.mylang文件蓝色 (或任何当前关键字颜色)。

How to create a simple coloring MEF VSIX extension 如何创建一个简单的着色MEF VSIX扩展

  1. Make sure you have the Visual Studio SDK installed. 确保安装了Visual Studio SDK。 ( VS2010 SP1 SDK , VS2012 SDK ) VS2010 SP1 SDKVS2012 SDK
  2. Create a new VSIX Project 创建一个新的VSIX项目
    (From the template under InstalledTemplatesVisual C#Extensibility .) (从已安装模板Visual C#可扩展性下的模板中 。)
  3. Enter something in the Author field of the VSIX manifest editor, then save and close it. 在VSIX清单编辑器的“ 作者”字段中输入内容,然后保存并关闭它。
  4. Add references to the following libraries, 添加对以下库的引用,
    version 10.0.0.0 for VS2010, or 11.0.0.0 for VS2012: VS2010版本10.0.0.0或VS2012版本11.0.0.0:

    • Microsoft.VisualStudio.CoreUtility.dll
    • Microsoft.VisualStudio.Language.StandardClassification.dll
    • Microsoft.VisualStudio.Text.Data.dll
    • Microsoft.VisualStudio.Text.Logic.dll
    • Microsoft.VisualStudio.Text.UI.dll
    • Microsoft.VisualStudio.Text.UI.Wpf.dll
  5. Add a reference to the following library: 添加对以下库的引用:

    • System.ComponentModel.Composition.dll version 4.0.0.0 System.ComponentModel.Composition.dll版本4.0.0.0
  6. Create and add a new code file MyLang.cs , and copy-and-paste the code below in it. 创建并添加新的代码文件MyLang.cs ,并将下面的代码复制并粘贴其中。

  7. Edit source.extension.vsixmanifest as XML. source.extension.vsixmanifest编辑为XML。

    • For Visual Studio 2010, add the following XML just before the closing tag </Vsix> , and save: 对于Visual Studio 2010,在结束标记</Vsix>之前添加以下XML,并保存:

       <Content> <MefComponent>|%CurrentProject%|</MefComponent> </Content> 

      (If there is already an empty <Content/> , remove it.) (如果已经有空<Content/> ,请将其删除。)

    • For Visual Stuio 2012, add the following XML just before the closing tag </PackageManifest> , and save: 对于Visual Stuio 2012,在结束标记</PackageManifest>之前添加以下XML,并保存:

       <Assets> <Asset Type="Microsoft.VisualStudio.MefComponent" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%|" /> </Assets> 

      (If there is already an empty <Assets/> , remove it.) (如果已有空<Assets/> ,请将其删除。)

  8. Only for Visual Studio 2010 : 仅适用于Visual Studio 2010

    • Unload the VSIX project (right-click the project → Unload project ). 卸载VSIX项目(右键单击项目→ 卸载项目 )。

    • Edit the .csproj project file (right-click the project → Edit MyProject.csproj ). 编辑.csproj项目文件(右键单击项目→ 编辑MyProject.csproj )。

    • Change the value at <IncludeAssemblyInVSIXContainer> to true . <IncludeAssemblyInVSIXContainer>的值更改为true

    • Save and close the file. 保存并关闭文件。

    • Reload the VSIX project (right-click the project → Reload project ). 重新加载VSIX项目(右键单击项目→ 重新加载项目 )。

  9. Now build and run it. 现在构建并运行它。 When you load a .mylang file, all text should be colored blue (or whatever the default keyword color is). 加载.mylang文件时,所有文本应为蓝色(或任何默认关键字颜色)。


MyLang.cs MyLang.cs

using Microsoft.VisualStudio.Language.StandardClassification;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Classification;
using Microsoft.VisualStudio.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;

namespace VSIXProject1
{
    internal static class MyLangLanguage
    {
        public const string ContentType = "mylang";

        public const string FileExtension = ".mylang";

        [Export]
        [Name(ContentType)]
        [BaseDefinition("code")]
        internal static ContentTypeDefinition MyLangSyntaxContentTypeDefinition = null;

        [Export]
        [FileExtension(FileExtension)]
        [ContentType(ContentType)]
        internal static FileExtensionToContentTypeDefinition MyLangSyntaxFileExtensionDefinition = null;
    }

    [Export(typeof(IClassifierProvider))]
    [ContentType(MyLangLanguage.ContentType)]
    [Name("MyLangSyntaxProvider")]
    internal sealed class MyLangSyntaxProvider : IClassifierProvider
    {
        [Import]
        internal IClassificationTypeRegistryService ClassificationRegistry = null;

        public IClassifier GetClassifier(ITextBuffer buffer)
        {
            return buffer.Properties.GetOrCreateSingletonProperty(() => new MyLangSyntax(ClassificationRegistry, buffer));
        }
    }

    internal sealed class MyLangSyntax : IClassifier
    {
        private ITextBuffer buffer;
        private IClassificationType identifierType;
        private IClassificationType keywordType;

        public event EventHandler<ClassificationChangedEventArgs> ClassificationChanged;

        internal MyLangSyntax(IClassificationTypeRegistryService registry, ITextBuffer buffer)
        {
            this.identifierType = registry.GetClassificationType(PredefinedClassificationTypeNames.Identifier);
            this.keywordType = registry.GetClassificationType(PredefinedClassificationTypeNames.Keyword);
            this.buffer = buffer;
            this.buffer.Changed += OnBufferChanged;
        }

        public IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan snapshotSpan)
        {
            var classifications = new List<ClassificationSpan>();

            string text = snapshotSpan.GetText();
            var span = new SnapshotSpan(snapshotSpan.Snapshot, snapshotSpan.Start.Position, text.Length);
            classifications.Add(new ClassificationSpan(span, keywordType));

            return classifications;
        }

        private void OnBufferChanged(object sender, TextContentChangedEventArgs e)
        {
            foreach (var change in e.Changes)
                ClassificationChanged(this, new ClassificationChangedEventArgs(new SnapshotSpan(e.After, change.NewSpan)));
        }
    }
}

Set <IncludeAssemblyInVSIXContainer> to true in the .csproj file, per this suggestion. 根据此建议,在.csproj文件中将<IncludeAssemblyInVSIXContainer>设置为true。

I had exactly the same problem and this solved it. 我有完全相同的问题,这解决了它。 Do a full rebuild. 做一个完整的重建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM