Zach Zach - 3 months ago 21
C# Question

Custom template namespace error

My custom project template uses Sidewaffle in Visual Studio. It works fine until I try to add a new class to the existing project.

Whenever a new class is added, the namespace is the same as the one in the project I used to create the template - as opposed to the one for the current project. The template replaces the namespace in the default files, just not the new ones.

Can I fix this or do I have to change the namespace each time?

Answer

So I found a workaround to the error. First I followed the steps outlined here: Proper way to rename solution (and directories) in Visual Studio. I figured I might have had a problem earlier when I renamed my solution that contained my template file.

That still didn't fix my problem because when looking at the default namespace under the properties section of the project it was the namespace in the project I used to create the template (as expected). However, Sidewaffle is supposed to take the namespaces and replace them with "$safeprojectname$". This doesn't seem to work for the default namespace, so whenever a new file is added, the incorrect namespace will be used.

Visual Studio doesn't let you enter "$safeprojectname$" for the default namespace because it says it's an invalid format. Long story short, you need to edit the .csproj file of the project that the template is modeled after (using any text editor)

`<?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>`

and change the "RootNamespace" property to "$safeprojectname$". I also changed the "AssemblyName" to "$safeprojectname$" for good measure. Save the file, and you should be good.