我想创建一个项目模板,使类名与用户提供的项目名称相匹配。
我的类被定义为(在导出的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;
}
}
}
如果我的ProjectName是ABCProj,那么我希望将TestClassViewModel创建为ABCProjViewModel。为了实现这一点,我将项目模板中的类文件更新为:
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作为TestClassViewModel。
我在这里做错了什么?
谢谢,
RDV
答案 0 :(得分:0)
我已经想出了一个更好的方法:
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