I try to get a menu entry in Visual Studio using PowerShell in the NuGet console or Power console. These PowerShell hosts run in the context of Visual Studio. I got somewhere, but not far enough... The menu entry appears (right-click on solution), but I can't get the event handler connected...
My code:
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($menui开发者_JAVA技巧tem)
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>
The last line gives the trouble...
I patterned this after code executed by nuget itself in nuget.psm1:
# 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
})
Any ideas on how to get the eventhandler connected?
精彩评论