【问题标题】:Using T4 Templates with VS2010 Express and XNA在 VS2010 Express 和 XNA 中使用 T4 模板
【发布时间】:2011-05-11 10:27:49
【问题描述】:

我正在使用 VS2010 express 创建一个使用 构建的游戏。我正在尝试使用 模板(生成强类型的内容位置类,因此使用Level1Location = Content.Levels.Level1 而不是Level1Location = @"Content\Levels\Level1"

我读到 T4 模板在 express 版本中没有正确设置,但是如果我创建一个扩展名为 .tt 的文件,它应该可以工作。但是,当我在我的 XNA 类库中创建一个 .tt 文件时,我收到以下警告(并且没有代码文件):

The custom tool 'TextTemplatingFileGenerator' failed. Could not load file or assembly 'Microsoft.VisualStudio.ServicesProxy, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

我已经搜索和搜索,但找不到任何有用的东西。有没有人遇到过这个问题?有谁知道有解决办法吗?

我也尝试按照建议将自定义工具更改为 TextTemplatingFilePreprocessor,但是我得到了同样的错误。

编辑:我发现问题在于它在 XNA 项目/库中。它在普通类中工作正常,所以我的工作是为模板添加一个项目到解决方案中。不过,这个问题仍然悬而未决,您能否让它在 XNA 项目中工作?

【问题讨论】:

  • 不应该是自定义工具的 TextTemplatingFilePreprocessor 吗?
  • 当我将扩展名更改为 .tt 时,它会自动放入 TextTemplatingFileGenerator。我试过 TextTemplatingFilePreprocessor 但我得到了同样的错误(更改了自定义工具名称)。
  • @Markust。一旦我更改了自定义工具,重新启动 VS2010 似乎已经解决了这个问题!谢谢。将此更改为答案,我会接受。编辑:嗯,我的错误我得到了同样的错误,但只有一次我尝试“运行自定义工具”
  • 您是否尝试过通过 Add->New Item->Text Template 添加模板,而不是手动重命名为 .tt ?
  • 该选项在 VS2010 express 中不可用。

标签: xna t4 c# xna t4 visual-studio-express


【解决方案1】:

虽然我已经发布了这个答案,但我仍在寻找一种更简单的方法(即 XNA 项目中的 tt 文件)

如果有人找到此页面,这是我的解决方法:

创建一个新的(非 XNA)类库项目。

添加一个文本文件,以 .tt 扩展名重命名。

编写您的 T4 代码(见下文)。

在您的 XNA 项目中,添加一个现有项目,导航到创建的 .cs 文件,然后添加为链接。

然后,为确保我们始终拥有更新的 cs 文件,右键单击 XNA 项目并单击项目依赖项,然后勾选包含 .tt 文件的类库项目。

使用下面的模板代码,您可以执行 Content.Load(Content.MyGameContent.Graphics.Textures.AwesomeTexture); 之类的操作您还可以使用 Content.MyGameContent.Graphics.Textures 将文件夹名称作为字符串获取,这要归功于时髦的隐式字符串转换运算符。

<#@ template language="c#" hostspecific="true" #>
<#@ assembly name="EnvDTE100" #>
<#@ assembly name="EnvDTE" #>
<#@ assembly name="System" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="EnvDTE100" #>
<#@ import namespace="EnvDTE" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections.Generic" #>
namespace Content
{
<#
var serviceProvider = this.Host as IServiceProvider;
var dte = serviceProvider.GetService(typeof(DTE)) as DTE;

foreach (Project Proj in dte.Solution.Projects)
{
    bool IsContentProj = false;
    foreach(Property Prop in Proj.Properties)
    {
        if(Prop.Name == "Microsoft.Xna.GameStudio.ContentProject.ContentRootDirectoryExtender.ContentRootDirectory")
        {
            IsContentProj = true;
        }
    }
    if (IsContentProj)
    {
#>
    public static class <#=Proj.Name #>
    {
<#
        foreach(ProjectItem PI in Proj.ProjectItems)
        {
GenerateProjectItemClass(PI, true, "        ");
        }
#>
    }
<#
    }
}
#>
}
<#+
    void GenerateProjectItemClass(ProjectItem Item, bool Static, string Indent)
    {
        const string FolderItemKind = "{6BB5F8EF-4483-11D3-8BCF-00C04F8EC28C}";
        const string FileItemKind = "{6BB5F8EE-4483-11D3-8BCF-00C04F8EC28C}";
        string ClassName = Path.GetDirectoryName(Item.FileNames[0]).Substring(Path.GetDirectoryName(Item.FileNames[0]).LastIndexOf(Path.DirectorySeparatorChar) + 1);
        int ContentRootLength = Path.GetDirectoryName(Item.ContainingProject.FileName).Length;

        string RelativeLocation = Path.ChangeExtension(Path.GetFullPath(Item.FileNames[0]).Substring(ContentRootLength + 1), null);

        switch(Item.Kind)
        {
            case FolderItemKind:
#>
<#=Indent#>public class <#=ClassName #>Class
<#=Indent#>{
<#=Indent#>    private const string Location = @"<#= RelativeLocation #>";
<#=Indent#>    public static implicit operator string(<#=ClassName #>Class MyClass)
<#=Indent#>    {
<#=Indent#>        return Location;
<#=Indent#>    }
<#+
            foreach(ProjectItem ChildItem in Item.ProjectItems)
                GenerateProjectItemClass(ChildItem, false, Indent + "    ");
#>
<#=Indent#>}
<#=Indent#>
<#=Indent#>public <#= Static ? "static " : " " #><#=ClassName#>Class <#=ClassName#> = new <#=ClassName#>Class();
<#=Indent#>
<#+
            break;
            case FileItemKind:
#>
<#=Indent#>public <#= Static ? "static " : " " #>string <#= Path.GetFileNameWithoutExtension(Item.FileNames[0]) #> = @"<#=RelativeLocation #>";
<#+
            break;
        }
    }
#>

【讨论】:

    猜你喜欢
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-20
    • 2016-11-01
    相关资源
    最近更新 更多