PowerShell 中的 Visual Studio 菜单项事件处理程序出现问题

发布于 2024-10-04 22:10:35 字数 1674 浏览 2 评论 0原文

我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文