简体   繁体   English

有没有办法在 Visual Studio 2022 中创建和使用 PowerShell 片段?

[英]Is there a way to create and use PowerShell snippets in Visual Studio 2022?

I do a lot of Winforms and WPF GUI design in Visual Studio and then further perfect my form in Visual Studio Code.我在 Visual Studio 中做了很多 Winforms 和 WPF GUI 设计,然后在 Visual Studio Code 中进一步完善我的表单。 (I'm using PowerShell Pro Tools from Ironman Studios) (我正在使用 Ironman Studios 的 PowerShell Pro Tools)

In VSCode, it's extremely easy to add and use snippets for PowerShell, especially with the Snippet Generator and Snippets Viewer addons installed:在 VSCode 中,为 PowerShell 添加和使用片段非常容易,尤其是在安装了Snippet GeneratorSnippets Viewer插件的情况下:

VSCode 片段管理器

I'm trying to replicate this somehow in Visual Studio Community 2022. I've defined a PowerShell snippet with the following code:我试图在 Visual Studio Community 2022 中以某种方式复制它。我使用以下代码定义了一个 PowerShell 片段:

<?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>

And I saved the file here: C:\Users\Username\Documents\Visual Studio 2022\Code Snippets\PowerShell\My Code Snippets\EnableVisualStyles.snippet我将文件保存在这里: C:\Users\Username\Documents\Visual Studio 2022\Code Snippets\PowerShell\My Code Snippets\EnableVisualStyles.snippet

Then, with a PowerShell file open, I right click and choose Snippet > Insert Snippet :然后,打开 PowerShell 文件,我右键单击并选择Snippet > Insert Snippet

插入片段

But I get an empty input box shown here:但我得到一个空的输入框,这里显示:

片段输入

No matter what I type in that box, for example the shortcut name of the snippet, I get no suggestions and if I press enter I get the following error:无论我在该框中输入什么,例如片段的快捷方式名称,我都没有收到任何建议,如果我按 Enter 键,我会收到以下错误:

错误

Am I missing something?我错过了什么吗? Is there any way to access PowerShell snippets in.ps1 files?有什么方法可以访问.ps1 文件中的 PowerShell 片段吗?

Is there some kind of extension on the marketplace that will add extended support for snippets?市场上是否有某种扩展可以增加对片段的扩展支持?

This is pretty crucial to me, as I depend heavily on my snippets library when developing.这对我来说非常重要,因为我在开发时非常依赖我的片段库。

Any help at all would be extremely welcomed.任何帮助都将受到极大的欢迎。

I too ran into this problem with Visual Studio 2022 Community Edition.我在使用 Visual Studio 2022 社区版时也遇到了这个问题。 Based on my experience with VS2022, it leaves too much to be desired.根据我对 VS2022 的经验,它还有很多不足之处。

You could try Downloading and Installing "PowerShell Tools for Visual Studio 2022" from the Visual Studio Marketplace (assuming you haven't done so already).您可以尝试从 Visual Studio Marketplace 下载并安装“适用于 Visual Studio 2022 的 PowerShell 工具”(假设您尚未这样做)。

PowerShell Tools for Visual Studio 2022: https://marketplace.visualstudio.com/items?itemName=AdamRDriscoll.PowerShellToolsVS2022 PowerShell Visual Studio 2022 工具: https://marketplace.visualstudio.com/items?itemName=AdamRDriscoll.PowerShellToolsVS2022

Even after installing the PowerShell Tools, I kept running into problems, as it seems Microsoft has gutted the majority of the PowerShell Assets, especially in relation to WPF and the like.即使在安装了 PowerShell 工具之后,我仍然遇到问题,因为微软似乎已经破坏了大部分 PowerShell 资产,尤其是与 WPF 等相关的资产。

Personally, I got tired of playing around with "Visual Studio 2022", which I ultimately Uninstalled and instead, Installed "Visual Studio 2019" in it's place.就个人而言,我厌倦了玩“Visual Studio 2022”,我最终卸载了它,而是在它的位置安装了“Visual Studio 2019”。

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

Much like VS2022, the "PowerShell Tools for Visual Studio" extension, that is compatible with VS2017 and VS2019 needs to be Installed.与 VS2022 非常相似,需要安装与 VS2017 和 VS2019 兼容的“适用于 Visual Studio 的 PowerShell 工具”扩展。

PowerShell Tools for Visual Studio (VS2017 & VS2019): https://marketplace.visualstudio.com/items?itemName=AdamRDriscoll.PowerShellToolsforVisualStudio2017-18561 PowerShell Visual Studio 工具(VS2017 和 VS2019): https://marketplace.visualstudio.com/items?itemName=AdamRDriscoll.PowerShellToolsforVisualStudio2017-18561

The major difference, between the VS2019 Version, is that the PowerShell Assets and Resources (including WPF, Forms, Scripting, Module, etc.) are still available. VS2019 版本之间的主要区别在于 PowerShell 资产和资源(包括 WPF、Forms、脚本、模块等)仍然可用。

I did do some research into the PowerShell Snippets and it seems that they are specific to "Visual Studio Code" only.我确实对 PowerShell Snippets 进行了一些研究,似乎它们仅特定于“Visual Studio Code”。

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

You could also look into "PowerShell Pro Tools" for Visual Studio, if you can afford to purchase a commercial extension.如果您有能力购买商业扩展,您还可以查看 Visual Studio 的“PowerShell Pro Tools”。

PowerShell Pro Tools: https://ironmansoftware.com/powershell-pro-tools PowerShell Pro Tools: https://ironmansoftware.com/powershell-pro-tools

That is all I have for now.这就是我现在所拥有的。 I hope this info helps you out.我希望这些信息对你有所帮助。 If I run into any other Information, that may be beneficial to you, I'll be sure to post an Update.如果我遇到任何其他可能对您有益的信息,我一定会发布更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM