[英]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.这是我的设置,现在必须保持这种状态。
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.