自定义模板命名空间错误

Custom template namespace error

我的自定义项目模板使用 Visual Studio 中的 Sidewaffle。它工作正常,直到我尝试向现有项目添加新的 class。

每当添加新的 class 时,名称空间与我用来创建模板的项目中的名称空间相同 - 而不是当前项目的名称空间。该模板替换了默认文件中的命名空间,而不是新文件。

我可以解决这个问题还是每次都必须更改命名空间?

所以我找到了错误的解决方法。首先,我遵循此处概述的步骤: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$”。保存文件,你应该没问题。