简体   繁体   English

Visual Studio模板-自定义参数

[英]Visual Studio Templates - custom parameters

I am using VS 2015 templates to create a project for me, within my C# classes I have substitutions like $projectname$ and they work great if I name my project like this - MyTemplatedProject . 我正在使用VS 2015模板为我创建一个项目,在我的C#类中,我有类似$projectname$类的替换项,如果我这样命名我的项目$projectname$ ,它们将非常MyTemplatedProject I use $projectname$ for the part of class names and namespaces. 我将$projectname$用于类名和名称空间。

If I create generate a class like $projectname$Context , it becomes MyTemplatedProjectContext and all is well. 如果我创建生成类似$projectname$Context的类,它将变为MyTemplatedProjectContext并且一切都很好。

But if I name my project MyTemplatedProject.FrontEnd I have problems with the classes that are generated because they have the . 但是,如果我将项目命名为MyTemplatedProject.FrontEnd ,则生成的类会出现问题,因为它们具有. in their name. 以他们的名字。

The substitution $projectname$Context becomes MyTemplatedProject.FrontEndContext and that does not work for a class name. 替换$projectname$Context变为MyTemplatedProject.FrontEndContext ,不适用于类名。

How do I create custom parameters based $projectname$ , ideally I would have a parameter like $projectnameUpToFirstDot$ which only returns MyTemplatedProject when the project name is MyTemplatedProject.FrontEndContext ? 如何创建自定义的参数,基于$projectname$ ,理想情况下我会像一个参数$projectnameUpToFirstDot$仅返回MyTemplatedProject当项目名称是MyTemplatedProject.FrontEndContext

If you just want to generate a valid class name based on the project name, instead of creating a custom template parameter, you can change $projectname$ to $safeprojectname$ , which is documented as: 如果只想基于项目名称生成有效的类名称,而不是创建自定义模板参数,则可以将$projectname$更改$projectname$ $safeprojectname$记录为:

The name provided by the user in the New Project dialog box, with all unsafe characters and spaces removed. 用户在“新建项目”对话框中提供的名称,其中删除了所有不安全的字符和空格。

I had exactly the same problem a week ago. 一周前我遇到了完全相同的问题。 I just needed some customed variables inside of my template, which would resolve in $projectname$ upper case and one other in lower case. 我只是在模板中需要一些自定义变量,这些变量将以$projectname$大写字母解析,而另一个以小写字母解析。 Things like that. 像这样的东西。

It turned out for me to be the most effective and flexible way to develop my own project wizard using VS' custom wizard template. 事实证明,这是使用VS的自定义向导模板开发自己的项目向导的最有效,最灵活的方法。

Unless JavaScript & HTML is not a no-go for you, it is pretty easy to have some string manipulation on variables like $projectname$ there. 除非您对JavaScript和HTML不陌生,否则对$projectname$类的变量进行一些字符串操作非常容易。

Once you set it up, go in default.js and edit in function 设置好之后,进入default.js并在函数中进行编辑

function onFinish()
{
    ...

    var prjName = wizard.FindSymbol('PROJECT_NAME');

    wizard.AddSymbol('PROJECT_NAME_WITHOUT_DOTS', prjName.split('.')[0]);

    ...
}

split('.') removes all dots and devides it into an array of strings. split('.')删除所有点并将其分成字符串数组。 With [0] you select just the part up to the first dot. 用[0],您只选择第一个点之前的部分。

In your template files you can then address the symbol PROJECT_NAME_WITHOUT_DOTS with the following syntax: 然后,您可以在模板文件中使用以下语法对符号PROJECT_NAME_WITHOUT_DOTS进行寻址:

class [!output PROJECT_NAME_WITHOUT_DOTS] {
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM