2017-03-17 47 views
0

我想創建一個項目模板,以便類名匹配用戶提供的項目名稱。更改VS項目模板中的類名稱

我的類定義爲(以出口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創建。爲了達到這個目的,我將項目模板中的類文件更新爲:

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

我已經想通了,做一個更好的辦法:

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