PowerShell 中的 Visual Studio 菜单项事件处理程序出现问题
我尝试使用 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
})
关于如何连接事件处理程序有什么想法吗?
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($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>
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论