简体   繁体   English

将T4模板与VS2010 Express和XNA一起使用

[英]Using T4 Templates with VS2010 Express and XNA

I'm using VS2010 express to create a game built with . 我正在使用VS2010 express来创建使用构建的游戏。 I'm trying to use templates (to produce strongly-typed classes of content locations, thus using Level1Location = Content.Levels.Level1 instead of Level1Location = @"Content\\Levels\\Level1" . 我正在尝试使用模板(以生成内容位置的强类型类,因此使用Level1Location = Content.Levels.Level1而不是Level1Location = @"Content\\Levels\\Level1"

I've read that T4 templates aren't properly set up in the express editions, but that if i create a file with extension .tt it should work. 我读过T4模板在快速版本中没有正确设置,但是如果我创建扩展名为.tt的文件,它应该可以工作。 However when i create a .tt file in my XNA class library i get the following warning (and no code file): 但是,当我在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.

I've searched and searched and can't find anything useful. 我已经搜索了很多,找不到有用的东西。 Has anyone encountered this problem before? 有人遇到过这个问题吗? Anyone know if a solution? 有人知道有解决办法吗?

I've also tried changing the custom tool to TextTemplatingFilePreprocessor as suggested, however i get the same error. 我还尝试按照建议将自定义工具更改为TextTemplatingFilePreprocessor ,但是出现相同的错误。

EDIT: I've discovered that the issue is that it's in an XNA project / library. 编辑: 我发现问题是它在XNA项目/库中。 It works fine in a normal class so my work around is to add a project to the solution just for the template. 它在正常的类中工作正常,所以我的解决方法是为解决方案添加仅针对模板的项目。 The question is still open though, can you get it to work within a XNA project? 但是问题仍然悬而未决,您可以在XNA项目中使用它吗?

Although i've posted this answer, i'm still looking for a simpler way of doing this (ie tt file in the XNA project its self) 尽管我已经发布了这个答案,但我仍在寻找一种更简单的方法(例如XNA项目中的tt文件本身)

In case anyone finds this page, here's my work around: 如果有人找到此页面,这是我的解决方法:

Create a new (non-XNA) class library project. 创建一个新的(非XNA)类库项目。

Add a text file, renamed with the .tt extension. 添加一个文本文件,扩展名为.tt。

Write your T4 code (see below). 编写您的T4代码(请参见下文)。

In your XNA project, add an existing item, navigate to the created .cs file, and add as link. 在您的XNA项目中,添加一个现有项目,导航到创建的.cs文件,并添加为链接。

Then, to ensure we always have the updated cs file, rightclick the XNA project and click project dependencies, then tick your class library project containing the .tt file. 然后,为确保我们始终拥有更新的CS文件,请右键单击XNA项目,然后单击项目依赖项,然后在包含.tt文件的类库项目中打勾。

Using the template code below, you can do things like Content.Load(Content.MyGameContent.Graphics.Textures.AwesomeTexture); 使用下面的模板代码,您可以执行类似Content.Load(Content.MyGameContent.Graphics.Textures.AwesomeTexture)的操作; You can also get folder names as strings with Content.MyGameContent.Graphics.Textures for example, thanks to a funky implicit string conversion operator. 例如,借助时髦的隐式字符串转换运算符,还可以使用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;
        }
    }
#>

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

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