在Visual Studio中创建项模板时如何获取项目名称?
在Visual Studio项模板中获取项目名称的解决方法
我在Visual Studio中创建项模板时,$rootnamespace$和$safeitemname$参数能正常替换,但使用$projectname$时,占位符没有被替换成实际项目名称,而是保留原样。我的模板文件如下:
UserControl.xaml
<UserControl x:Class="$rootnamespace$.$safeitemname$" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" xmlns:viewModels="clr-namespace:$projectname$.ViewModels" xmlns:converters="clr-namespace:$projectname$.Converters" xmlns:helpers="clr-namespace:$projectname$.Helpers" xmlns:i="http://schemas.microsoft.com/xaml/behaviors" xmlns:local="clr-namespace:$rootnamespace$" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <UserControl.DataContext> <viewModels:$safeitemname$ViewModel /> </UserControl.DataContext> <Grid> </Grid> </UserControl>
UserControl.xaml.cs
namespace $rootnamespace$ { public partial class $safeitemname$ : UserControl { public $safeitemname$() { InitializeComponent(); } } }
UserControl.vstemplate
<?xml version="1.0" encoding="utf-8"?> <VSTemplate Version="3.0.0" Type="Item" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.microsoft.com/developer/vstemplate/2005"> <TemplateData> <Name Package="{b3bae735-386c-4030-8329-ef48eeda4036}" ID="4664">SUserControl</Name> <Description Package="{b3bae735-386c-4030-8329-ef48eeda4036}" ID="4665"></Description> <Icon Package="{b3bae735-386c-4030-8329-ef48eeda4036}" ID="4594" /> <TemplateID>Microsoft.CSharp.WPF.UserControl</TemplateID> <ProjectType>CSharp</ProjectType> <AppliesTo>CSharp + (WPF | (!CPS + WindowsForms))</AppliesTo> <ShowByDefault>false</ShowByDefault> <SortOrder>98</SortOrder> <RequiredFrameworkVersion>3.0</RequiredFrameworkVersion> <NumberOfParentCategoriesToRollUp>1</NumberOfParentCategoriesToRollUp> <DefaultName>SUserControl.xaml</DefaultName> <AppIdFilter>blend</AppIdFilter> </TemplateData> <TemplateContent> <References> <Reference> <Assembly>System</Assembly> </Reference> <Reference> <Assembly>System.Xaml</Assembly> </Reference> <Reference> <Assembly>WindowsBase</Assembly> </Reference> <Reference> <Assembly>PresentationCore</Assembly> </Reference> <Reference> <Assembly>PresentationFramework</Assembly> </Reference> </References> <ProjectItem ReplaceParameters="true" CustomTool="MSBuild:Compile">SUserControl1.xaml</ProjectItem> <ProjectItem ReplaceParameters="true" SubType="Code" TargetFileName="$safeitemname$.xaml.cs">SUserControl1.xaml.cs</ProjectItem> </TemplateContent> </VSTemplate>
解决方法
Visual Studio项模板没有内置的$projectname$参数,这就是它无法被替换的原因。可以通过以下两种方式解决:
方法1:利用$rootnamespace$参数(推荐)
通常项目的根命名空间($rootnamespace$)和项目名称一致(除非手动修改过)。直接把模板中的$projectname$替换为$rootnamespace$即可:
修改UserControl.xaml中的命名空间引用:
xmlns:viewModels="clr-namespace:$rootnamespace$.ViewModels" xmlns:converters="clr-namespace:$rootnamespace$.Converters" xmlns:helpers="clr-namespace:$rootnamespace$.Helpers"
如果根命名空间包含子命名空间(比如MyProject.UI),而项目名称是MyProject,可以通过自定义模板参数提取根部分,但这种场景比较少见。
方法2:添加自定义参数(兼容性有限)
如果必须明确获取项目名称,可以尝试在VSTemplate文件的TemplateContent节点下添加自定义参数:
<CustomParameters> <CustomParameter Name="$projectname$" Value="$projectname$"/> </CustomParameters>
不过这个方法在部分VS版本中可能不生效,因为项模板本身无法直接获取项目名称,更可靠的还是方法1。
验证修改
修改后重新导出项模板,创建新项时,$rootnamespace$会自动替换为项目的根命名空间,从而间接实现项目名称的引用。
内容的提问来源于stack exchange,提问作者Ozgur Saklanmaz
相关产品推荐
相关产品推荐

