如何在 VsPackage 中使用 class 库
How to use class library in VsPackage
以下场景。我在 VS 2017 中创建了一个新的 VSIX 项目。我在那里插入了一个新工具 Window。此外,我还创建了一个 class 库。生成的结构如下:
我唯一改变的是
我向 ToolWindow1Package
class 添加了两个属性,以便在我加载解决方案时立即加载它
[ProvideAutoLoad(VSConstants.UICONTEXT.SolutionHasSingleProject_string)]
[ProvideAutoLoad(VSConstants.UICONTEXT.SolutionHasMultipleProjects_string)]
Initialize
方法
protected override void Initialize()
{
ToolWindow1Command.Initialize(this);
base.Initialize();
Class1 class1 = new Class1();
Console.WriteLine(class1);
}
当我加载解决方案时,程序包无法加载,并且 ActivityLog 中有以下条目
<entry>
<record>1396</record>
<time>2018/01/10 15:16:12.081</time>
<type>Error</type>
<source>VisualStudio</source>
<description>LegacySitePackage failed for package [ToolWindow1Package]Source: 'TestExtension' Description: Could not load file or assembly 'TestLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. A strongly-named assembly is required. (Exception from HRESULT: 0x80131044)
System.IO.FileLoadException: Could not load file or assembly 'TestLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. A strongly-named assembly is required. (Exception from HRESULT: 0x80131044)
File name: 'TestLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
 at TestExtension.ToolWindow1Package.Initialize()
 at Microsoft.VisualStudio.Shell.Package.Microsoft.VisualStudio.Shell.Interop.IVsPackage.SetSite(IServiceProvider sp)

</description>
<guid>{673860A0-BAF8-46CB-BDFD-F758C4C6EE3C}</guid>
<hr>80131044</hr>
<errorinfo></errorinfo>
库的dll仍然在扩展dll的文件夹中:
我什至尝试将库作为资产添加到 .vsixmanifest 中,但情况没有任何变化。有人知道这个问题和解决方法吗?
您必须通过向项目添加 key.snk 文件来对 TestLibrary dll/project 进行强签名,如错误消息所述。 (项目属性,签名)
以下场景。我在 VS 2017 中创建了一个新的 VSIX 项目。我在那里插入了一个新工具 Window。此外,我还创建了一个 class 库。生成的结构如下:
我唯一改变的是
我向
ToolWindow1Package
class 添加了两个属性,以便在我加载解决方案时立即加载它[ProvideAutoLoad(VSConstants.UICONTEXT.SolutionHasSingleProject_string)] [ProvideAutoLoad(VSConstants.UICONTEXT.SolutionHasMultipleProjects_string)]
Initialize
方法protected override void Initialize() { ToolWindow1Command.Initialize(this); base.Initialize(); Class1 class1 = new Class1(); Console.WriteLine(class1); }
当我加载解决方案时,程序包无法加载,并且 ActivityLog 中有以下条目
<entry>
<record>1396</record>
<time>2018/01/10 15:16:12.081</time>
<type>Error</type>
<source>VisualStudio</source>
<description>LegacySitePackage failed for package [ToolWindow1Package]Source: 'TestExtension' Description: Could not load file or assembly 'TestLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. A strongly-named assembly is required. (Exception from HRESULT: 0x80131044)
System.IO.FileLoadException: Could not load file or assembly 'TestLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. A strongly-named assembly is required. (Exception from HRESULT: 0x80131044)
File name: 'TestLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
 at TestExtension.ToolWindow1Package.Initialize()
 at Microsoft.VisualStudio.Shell.Package.Microsoft.VisualStudio.Shell.Interop.IVsPackage.SetSite(IServiceProvider sp)

</description>
<guid>{673860A0-BAF8-46CB-BDFD-F758C4C6EE3C}</guid>
<hr>80131044</hr>
<errorinfo></errorinfo>
库的dll仍然在扩展dll的文件夹中:
我什至尝试将库作为资产添加到 .vsixmanifest 中,但情况没有任何变化。有人知道这个问题和解决方法吗?
您必须通过向项目添加 key.snk 文件来对 TestLibrary dll/project 进行强签名,如错误消息所述。 (项目属性,签名)