我尝试在NuGet控制台或Power控制台中使用PowerShell在Visual Studio中获取菜单条目。这些PowerShell主机在Visual Studio的上下文中运行。我到了某个地方,但还不够......显示菜单条目(右键单击解决方案),但我无法连接事件处理程序......
我的代码:
PM> $sol = $DTE.CommandBars | Where-Object { $_.Name -like 'Solution' }
PM> $menuitem = $sol.Controls.Add([Microsoft.VisualStudio.CommandBars.MsoControlType]::msoControlButton, 1, "", 1, $true)
PM> $menuitem.Caption = "Action From NuGet"
PM> $menuItemHandler = $DTE.Events.CommandBarEvents($menuitem)
PM> $commandBarEvents = Get-Interface $menuItemHandler ([EnvDTE._dispCommandBarControlEvents_Event])
PM> $commandBarEvents.add_Click([EnvDTE._dispCommandBarControlEvents_ClickEventHandler]{Write-Host “Clicked”})
Cannot convert value "Write-Host “Clicked”" to type "EnvDTE._dispCommandBarControlEvents_ClickEventHandler". Error: "The type 'System.Boolean&' may not be used as a type argument."
At line:1 char:84
+ $commandBarEvents.add_Click([EnvDTE._dispCommandBarControlEvents_ClickEventHandler] <<<< {Write-Host “Clicked”})
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException
PM>
最后一行给出了麻烦......
我在nuget.psm1:
中由nuget本身执行的代码后对此进行了模式化# Hook up Solution events
$solutionEvents = Get-Interface $dte.Events.SolutionEvents ([EnvDTE._dispSolutionEvents_Event])
$solutionEvents.add_Opened([EnvDTE._dispSolutionEvents_OpenedEventHandler]{
ExecuteInitScripts
UpdateWorkingDirectory
})
$solutionEvents.add_AfterClosing([EnvDTE._dispSolutionEvents_AfterClosingEventHandler]{
UpdateWorkingDirectory
})
有关如何连接事件处理程序的任何想法?