如何以编程方式将SDK添加到UWP项目中?

时间:2017-05-22 11:10:46

标签: nuget visual-studio-extensions vsix vs-extensibility visual-studio-templates

我正在为通用Windows平台开发项目模板。在UWP项目中,我们可以通过三种方式添加引用,即Assemblies,NuGet和SDK。要添加程序集,我只需使用

VSProject.References.Add("MyAssembly.dll")

我可以使用NuGet.VisualStudio dll安装NuGet包。但是如何以编程方式将SDK添加到UWP项目中,如引用和NuGet包?

enter image description here

2 个答案:

答案 0 :(得分:1)

您可以使用Microsoft.Build.Evaluation.Project的additem方法来实现它。这是一个示例演示供您参考。

DTE2 dte = this.ServiceProvider.GetService(typeof(DTE)) as DTE2;
EnvDTE.Project currentProject = dte.Solution.Projects.Item(1);
string projectPath = currentProject.FullName;
var project = new Microsoft.Build.Evaluation.Project(projectPath);

var proItem = project.GetItems("SDKReference").FirstOrDefault();

project.AddItem("SDKReference", "WindowsTeam, Version=10.0.14393.0", new[]
                        {
                            new KeyValuePair<string, string>("Name", "Windows Team Extensions for the UWP")
                        });

project.Save();

更新

DTE2 dte = this.ServiceProvider.GetService(typeof(DTE)) as DTE2;
EnvDTE.Project currentProject = dte.Solution.Projects.Item(1);
string projectPath = currentProject.FullName;

Microsoft.Build.Evaluation.ProjectCollection projectCollection = new Microsoft.Build.Evaluation.ProjectCollection();

var project = projectCollection.LoadProject(projectPath);             projectCollection.UnloadProject(project);

project.AddItem("SDKReference", "WindowsTeam, Version=10.0.14393.0", new[]
                        {
                            new KeyValuePair<string, string>("Name", "Windows Team Extensions for the UWP")
                        });

project.Save();

projectCollection.LoadProject(projectPath);

答案 1 :(得分:0)

我已将sdk条目添加到替换功能中,如下所示,并在模板的项目文件中添加了一个查找字符串$ myuwpsdk $。它按预期工作。这可能是一种不正确的方法或临时解决方法。但它完成了我的要求。

replacementsDictionary["$myuwpsdk$"] = "<ItemGroup><SDKReference Include = \"WindowsTeam, Version=10.0.14393.0\"> <Name>Windows Team Extensions for the UWP</Name> </SDKReference> </ItemGroup>";