创建可以处理 T4 文本模板的 Visual Studio 2022 (VSIX) 扩展

问题描述 投票:0回答:1

我一直在尝试创建一个可以处理文本模板(T4)的 Visual Studio 扩展(带命令)。

我一直在尝试遵循此页面,其中标题为“将参数值传递到模板”。我尝试将该标题下的代码复制到“Command”文件中,但收到有关 dte 变量的错误。谁能帮我解决这个问题,因为我对 Visual Studio 扩展完全陌生?我不确定如何“获取服务提供商”或“DTE”是什么,我认为让事情变得更加困难的是它是一种异步方法。

这是我在扩展名的命令文件中的代码:

using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.TextTemplating;
using Microsoft.VisualStudio.TextTemplating.VSHost;
using System.IO;

namespace TestVSIXExtension
{
    [Command(PackageIds.MyCommand)]
    internal sealed class MyCommand : BaseCommand<MyCommand>
    {
        protected override async Task ExecuteAsync(OleMenuCmdEventArgs e)
        {
                // Get a service provider - how you do this depends on the context:
                IServiceProvider serviceProvider = dte; // or dslDiagram.Store, for example
                                                        // Get the text template service:
                ITextTemplating t4 = serviceProvider.GetService(typeof(STextTemplating)) as ITextTemplating;
                ITextTemplatingSessionHost host = t4 as ITextTemplatingSessionHost;
                // Create a Session in which to pass parameters:
                host.Session = host.CreateSession();
                // Add parameter values to the Session:
                session["TimesToRepeat"] = 5;
                // Process a text template:
                string result = t4.ProcessTemplate("MyTemplateFile.t4",
                  System.IO.File.ReadAllText("MyTemplateFile.t4"));
           
            await VS.MessageBox.ShowWarningAsync("TestVSIXExtension", "Button clicked");
        }
    }
}
c# visual-studio-2022 vsix envdte vsixmanifest
1个回答
0
投票

花了几个小时才找到解决方案,但就在这里。遗憾的是没有关于如何执行此操作的更新教程。

 using EnvDTE;
    using EnvDTE80;
    using Microsoft.VisualStudio.Shell;
    using Microsoft.VisualStudio.Shell.Interop;
    using Microsoft.VisualStudio.TextTemplating;
    using Microsoft.VisualStudio.TextTemplating.VSHost;
    using System.IO;
    
    namespace TestVSIXExtension
    {
        [Command(PackageIds.MyCommand)]
        internal sealed class MyCommand : BaseCommand<MyCommand>
        {
            protected override async Task ExecuteAsync(OleMenuCmdEventArgs e)
            {
                    // Get the text template service:
                    ITextTemplating t4 = await Package.GetServiceAsync(typeof(STextTemplating)) as ITextTemplating;
                    ITextTemplatingSessionHost host = t4 as ITextTemplatingSessionHost;
                    // Create a Session in which to pass parameters:
                    host.Session = host.CreateSession();
                    // Add parameter values to the Session:
                    session["TimesToRepeat"] = 5;
                    // Process a text template:
                    string result = t4.ProcessTemplate("MyTemplateFile.t4",
                      System.IO.File.ReadAllText("MyTemplateFile.t4"));
               
                await VS.MessageBox.ShowWarningAsync("TestVSIXExtension", "Button clicked");
            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.