简体   繁体   English

Nuget.VisualStudio.IVsPackageInstaller如何使用?

[英]How to use Nuget.VisualStudio.IVsPackageInstaller?

I am looking everywhere and nothing works, nor is it complete enough to use.我到处都在寻找,但没有任何效果,也不够完整,无法使用。

I want to dynamically install packages from a Custom Template.我想从自定义模板动态安装包。 I have already used the element of the vsTemplate file, but I would like to be able to use the NuGet API to do it more dynamically.我已经使用了 vsTemplate 文件的元素,但我希望能够使用 NuGet API 来更动态地完成它。

This is my set up, and for now it HAS to stay this way.这是我的设置,现在必须保持这种状态。

  • Visual Studio 2019 Enterprise Visual Studio 2019 企业版
  • EnvDTE (8.0.1)环境 DTE (8.0.1)
  • EnvDTE80 (8.0.1)环境DTE80 (8.0.1)
  • Nuget.VisualStudio (5.11.3) Nuget.VisualStudio (5.11.3)
  • VSLangProj (7.0.3300) VSLang 项目 (7.0.3300)

With that, I have the following methods on the IWizard这样,我在 IWizard 上有以下方法

public DTE2 Application {get;set;}
public Project Project { get; private set; }

public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
{
   this.Application = (DTE2)automoationObject;
}

public void ProjectItemFinishedGenerating(ProjectItem projectItem)
{
  this.Project = projectItem?.ContainingProject;
}

public void RunFinished() 
{
    IVsPackageInstaller installer;
    
    //Get Nuget Package Installer

    installer.InstallPackage(null, this.Project, "MyCustomPackage", "1.0.0", false);
}

Where i put "get nuget package installer" what do i put in there?我把“获取 nuget package 安装程序”放在哪里?
Everywhere i search i get this "GetService" but not where that method lives, or how to access it, at the Nuget.VisualStudio version 5.11.3.在 Nuget.VisualStudio 版本 5.11.3 中,我在任何地方搜索都会得到此“GetService”,但找不到该方法所在的位置或如何访问它。 I Cannot update to later versions at this time, but so far every suggestion I find on the.net doesn't work.我现在无法更新到更高版本,但到目前为止,我在 .net 上找到的所有建议都没有用。

From some public projects using this, I found them Call GetService() from the IComponentModel, just like this:从使用它的一些公共项目中,我发现它们从 IComponentModel 调用 GetService(),就像这样:

using System;

namespace FrameworkInstallPackage
{
    using Microsoft.VisualStudio.ComponentModelHost;
    using NuGet.VisualStudio;
    using TPL = System.Threading.Tasks;
    using Microsoft.VisualStudio.Shell;
    using EnvDTE;

    internal interface IPackageHandler
    {
        /// <summary>
        /// Gets the VS package
        /// </summary>
        AsyncPackage Package { get; } //the root class AsyncPackage comes from Microsoft.VisualStudio.Shell

        /// <summary>
        /// Executes the function
        /// </summary>
        /// <param name="project">The project to peform actions on</param>
        /// <returns>A task that executes the function</returns>
        TPL.Task Execute(Project project);
    }
    internal abstract class BasePackageHandler : IPackageHandler
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="BasePackageHandler"/> class.
        /// </summary>
        /// <param name="successor">The successor</param>
        protected BasePackageHandler(IPackageHandler successor)
        {
            this.Successor = successor ?? throw new ArgumentNullException(nameof(successor));
            if (successor.Package == null)
            {
                throw new ArgumentException("successor.Package must not be null");
            }

            this.Package = this.Successor.Package;
        }

        /// <summary>
        /// Gets the VS package
        /// </summary>
        public AsyncPackage Package { get; }

        /// <summary>
        /// Gets the successor handler
        /// </summary>
        protected IPackageHandler Successor { get; }

        /// <inheritdoc/>
        public abstract TPL.Task Execute(Project project);
    }
    /// <summary>
    /// Installs the latest SlowCheetah NuGet package
    /// </summary>
    internal class NugetInstaller : BasePackageHandler
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="NugetInstaller"/> class.
        /// </summary>
        /// <param name="successor">The successor with the same package</param>
        public NugetInstaller(IPackageHandler successor)
            : base(successor)
        {
        }

        /// <inheritdoc/>
        public override async TPL.Task Execute(Project project)
        {
            var componentModel = (IComponentModel)await this.Package.GetServiceAsync(typeof(SComponentModel));
            IVsPackageInstaller packageInstaller = componentModel.GetService<IVsPackageInstaller>();
        }
    }
}

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

相关问题 使用Nuget.VisualStudio安装NuGet包时如何生成绑定重定向? - How do I generate binding redirects when using Nuget.VisualStudio to install a NuGet package? .Net / VisualStudio如何处理具有相同nuget包的项目依赖项,每个项目具有不同的nuget目标 - How .Net/VisualStudio handles having projects dependencies each with different nuget target for same nuget packages VisualStudio Online并打包Nuget包 - VisualStudio Online and pack Nuget packages 如何使用NuGet? - How to use NuGet? 如何(正确)使用此VisualStudio全局变量? - How to (properly) use this VisualStudio global variable? 如何使用Nuget.VisualStudio从存储库中获取软件包列表 - Using Nuget.VisualStudio how would I get a list of packages from a repository "如何在 nuget 中使用外部别名" - How to use extern alias with nuget 在源代码上找不到IVsPackageInstaller软件包 - IVsPackageInstaller package not found on source 如何使用VisualStudio构建配置为dotnet Core项目定义DEBUG? - How to use VisualStudio build configuration to define DEBUG for dotnet Core project? 如何在单元测试应用程序中使用Microsoft.VisualStudio.TestPlatform.TestExecutor? - How to use Microsoft.VisualStudio.TestPlatform.TestExecutor in Unit Test App?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM