在 VS 项目模板中更改 class 名称
Change class name in VS project template
我想创建一个项目模板,使 class 名称与用户提供的项目名称匹配。
我的 class 定义为(在导出的 ProjectTemplate 中):
namespace $safeprojectname$.ViewModels
{
[Export("$safeprojectname$.ViewModels.TestClassViewModel ",typeof(ContentPaneViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class TestClassViewModel : ContentPaneViewModel
{
[ImportingConstructor]
public TestClassViewModel ([Import("$safeprojectname$.Views.TestClassView")]IView theView)
{
View = theView;
View.ViewModel = this;
}
}
}
如果我的项目名称是 ABCProj,那么我希望将 TestClassViewModel 创建为 ABCProjViewModel。为此,我将项目模板中的 class 文件更新为:
namespace $safeprojectname$.ViewModels
{
[Export("$safeprojectname$.ViewModels.$safeprojectname$",typeof(ContentPaneViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class $safeprojectname$: ContentPaneViewModel
{
[ImportingConstructor]
public $safeprojectname$([Import("$safeprojectname$.Views.TestClassView")]IView theView)
{
View = theView;
View.ViewModel = this;
}
}
}
保存更改并重新创建项目模板 zip 文件。但是当我使用这个模板创建一个项目时,我仍然得到 classname as TestClassViewModel。
我这里做错了什么?
谢谢,
RDV
我找到了更好的方法:
1. Install visual studio extensibility tools for creating templates.
2. Create Project & Item templates separately.
3. In Project template just create folders, references, and resource dictionary, if any.
4. Create class files in item template
5. Make sure when putting a variable like $safeitemname$ or $projectname$, make sure .vstemplate & project.csproj has the same filename.
谢谢,
RDV
我想创建一个项目模板,使 class 名称与用户提供的项目名称匹配。
我的 class 定义为(在导出的 ProjectTemplate 中):
namespace $safeprojectname$.ViewModels
{
[Export("$safeprojectname$.ViewModels.TestClassViewModel ",typeof(ContentPaneViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class TestClassViewModel : ContentPaneViewModel
{
[ImportingConstructor]
public TestClassViewModel ([Import("$safeprojectname$.Views.TestClassView")]IView theView)
{
View = theView;
View.ViewModel = this;
}
}
}
如果我的项目名称是 ABCProj,那么我希望将 TestClassViewModel 创建为 ABCProjViewModel。为此,我将项目模板中的 class 文件更新为:
namespace $safeprojectname$.ViewModels
{
[Export("$safeprojectname$.ViewModels.$safeprojectname$",typeof(ContentPaneViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class $safeprojectname$: ContentPaneViewModel
{
[ImportingConstructor]
public $safeprojectname$([Import("$safeprojectname$.Views.TestClassView")]IView theView)
{
View = theView;
View.ViewModel = this;
}
}
}
保存更改并重新创建项目模板 zip 文件。但是当我使用这个模板创建一个项目时,我仍然得到 classname as TestClassViewModel。
我这里做错了什么?
谢谢,
RDV
我找到了更好的方法:
1. Install visual studio extensibility tools for creating templates.
2. Create Project & Item templates separately.
3. In Project template just create folders, references, and resource dictionary, if any.
4. Create class files in item template
5. Make sure when putting a variable like $safeitemname$ or $projectname$, make sure .vstemplate & project.csproj has the same filename.
谢谢,
RDV