[英]Using T4 Templates with VS2010 Express and XNA
我正在使用VS2010 express來創建使用xna構建的游戲。 我正在嘗試使用t4模板(以生成內容位置的強類型類,因此使用Level1Location = Content.Levels.Level1
而不是Level1Location = @"Content\\Levels\\Level1"
。
我讀過T4模板在快速版本中沒有正確設置,但是如果我創建擴展名為.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項目中使用它嗎?
盡管我已經發布了這個答案,但我仍在尋找一種更簡單的方法(例如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;
}
}
#>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.