【问题标题】:Custom template namespace error自定义模板命名空间错误
【发布时间】:2016-08-19 16:52:25
【问题描述】:
我的自定义项目模板在 Visual Studio 中使用 Sidewaffle。在我尝试将新类添加到现有项目之前,它工作正常。
每当添加一个新类时,命名空间都与我用于创建模板的项目中的命名空间相同 - 而不是当前项目的命名空间。模板替换默认文件中的命名空间,而不是新文件。
我可以解决这个问题还是每次都必须更改命名空间?
【问题讨论】:
标签:
c#
visual-studio
project-template
sidewaffle
【解决方案1】:
所以我找到了解决该错误的方法。首先,我按照此处概述的步骤操作:Proper way to rename solution (and directories) in Visual Studio。当我重命名包含我的模板文件的解决方案时,我认为我之前可能遇到了问题。
这仍然没有解决我的问题,因为在查看项目属性部分下的默认命名空间时,它是我用来创建模板的项目中的命名空间(如预期的那样)。但是,Sidewaffle 应该采用命名空间并将它们替换为“$safeprojectname$”。这似乎不适用于默认命名空间,因此无论何时添加新文件,都会使用不正确的命名空间。
Visual Studio 不允许您为默认命名空间输入“$safeprojectname$”,因为它说这是一种无效格式。长话短说,您需要编辑模板所依据的项目的 .csproj 文件(使用任何文本编辑器)
`<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{88B0B111-3A80-4289-9B81-C7323331ABCB}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>$safeprojectname$</RootNamespace>
<AssemblyName>$safeprojectname$</AssemblyName>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>`
并将“RootNamespace”属性更改为“$safeprojectname$”。我还将“AssemblyName”更改为“$safeprojectname$”,以取得良好的效果。保存文件,你应该很好。