【问题标题】:Is there a way to create and use PowerShell snippets in Visual Studio 2022?有没有办法在 Visual Studio 2022 中创建和使用 PowerShell 片段?
【发布时间】:2022-08-07 14:17:10
【问题描述】:

我在 Visual Studio 中做了很多 Winforms 和 WPF GUI 设计,然后在 Visual Studio Code 中进一步完善我的表单。 (我正在使用 Ironman Studios 的 PowerShell Pro Tools)

在 VSCode 中,为 PowerShell 添加和使用 sn-ps 非常容易,尤其是安装了 Snippet GeneratorSnippets Viewer 插件后:

我试图在 Visual Studio Community 2022 中以某种方式复制它。我已经使用以下代码定义了一个 PowerShell sn-p:

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<CodeSnippets xmlns=\"http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet\">
    <CodeSnippet Format=\"1.0.0\">
        <Header>
            <Title>GUI Enable Visual Styles</Title>
            <Description>Enable Visual Styles</Description>
            <Shortcut>enablevs</Shortcut>
        </Header>
        <Snippet>
            <Code Language=\"PowerShell\">
                <![CDATA[
                Add-Type -AssemblyName System.Windows.Forms
                Add-Type -AssemblyName System.Drawing
                Add-Type -AssemblyName PresentationCore,PresentationFramework
                [System.Windows.Forms.Application]::EnableVisualStyles()
                ]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

我把文件保存在这里:C:\\Users\\Username\\Documents\\Visual Studio 2022\\Code Snippets\\PowerShell\\My Code Snippets\\EnableVisualStyles.snippet

然后,打开一个 PowerShell 文件,我右键单击并选择 Snippet &gt; Insert Snippet

但我得到一个空的输入框,这里显示:

无论我在该框中输入什么,例如 sn-p 的快捷方式名称,我都没有收到任何建议,如果我按 Enter 键,我会收到以下错误:

我错过了什么吗?有什么方法可以访问 PowerShell sn-ps in.ps1 文件?

市场上是否有某种扩展可以增加对 sn-ps 的扩展支持?

这对我来说非常重要,因为我在开发时非常依赖我的 sn-ps 库。

任何帮助都将受到极大的欢迎。

    标签: visual-studio code-snippets visual-studio-extensions visual-studio-2022


    【解决方案1】:

    我在使用 Visual Studio 2022 社区版时也遇到了这个问题。根据我对 VS2022 的经验,它还有很多不足之处。

    您可以尝试从 Visual Studio Marketplace 下载并安装“适用于 Visual Studio 2022 的 PowerShell 工具”(假设您尚未这样做)。

    适用于 Visual Studio 2022 的 PowerShell 工具:https://marketplace.visualstudio.com/items?itemName=AdamRDriscoll.PowerShellToolsVS2022

    即使在安装了 PowerShell 工具之后,我仍然遇到问题,因为微软似乎已经破坏了大部分 PowerShell 资产,尤其是与 WPF 等相关的资产。

    就个人而言,我厌倦了玩“Visual Studio 2022”,我最终卸载了它,而是在它的位置安装了“Visual Studio 2019”。

    Visual Studio 2019 下载:https://docs.microsoft.com/en-us/visualstudio/releases/2019/release-notes

    与 VS2022 非常相似,需要安装与 VS2017 和 VS2019 兼容的“适用于 Visual Studio 的 PowerShell 工具”扩展。

    适用于 Visual Studio 的 PowerShell 工具(VS2017 和 VS2019):https://marketplace.visualstudio.com/items?itemName=AdamRDriscoll.PowerShellToolsforVisualStudio2017-18561

    VS2019 版本之间的主要区别在于 PowerShell 资产和资源(包括 WPF、表单、脚本、模块等)仍然可用。

    我确实对 PowerShell Snippets 做了一些研究,它们似乎只针对“Visual Studio Code”。

    PowerShell(Visual Studio 代码):https://marketplace.visualstudio.com/items?itemName=ms-vscode.PowerShell

    如果您有能力购买商业扩展,您还可以查看 Visual Studio 的“PowerShell Pro Tools”。

    PowerShell 专业工具:https://ironmansoftware.com/powershell-pro-tools

    这就是我现在所拥有的。我希望这些信息对你有所帮助。 如果我遇到任何其他可能对您有益的信息,我一定会发布更新。

    【讨论】:

      猜你喜欢
      • 2021-12-30
      • 1970-01-01
      • 2014-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-19
      • 1970-01-01
      • 2022-07-04
      相关资源
      最近更新 更多