English 中文(简体)
采用T4
原标题:Generating WMAppManifest.xml with T4

I ve a section that only Return the Value in AssemblyInfo.cs (该代码为Windows Telephone):

using System.Runtime.InteropServices;
using System.Reflection;

namespace Tiletoons
{
    class AppInfo
    {
        public static readonly string Id = string.Empty;
        public static readonly string Product = string.Empty;
        public static readonly string Company = string.Empty;
        public static readonly string Version = string.Empty;

        static AppInfo()
        {
            Assembly assembly = Assembly.GetExecutingAssembly();

            foreach (object attribute in assembly.GetCustomAttributes(false)) {
                if (attribute.GetType() == typeof(GuidAttribute)) {
                    Id = (attribute as GuidAttribute).Value;
                } else if (attribute.GetType() == typeof(AssemblyProductAttribute)) {
                    Product = (attribute as AssemblyProductAttribute).Product;
                } else if (attribute.GetType() == typeof(AssemblyCompanyAttribute)) {
                    Company = (attribute as AssemblyCompanyAttribute).Company;
                } 
            }

            Version = assembly.FullName.Split( = )[1].Split( , )[0];
        }
    }
}

...... 我愿通过T4变革自动产生我的WMAppManifest.xml。

<#@ assembly name="System.Core" #>
<#@ assembly name="EnvDTE" #>
<#@ import namespace="EnvDTE" #>
<#@ import namespace="System" #>
<# 
IServiceProvider hostServiceProvider = Host as IServiceProvider;
EnvDTE.DTE dte = hostServiceProvider.GetService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
EnvDTE.ProjectItem containingProjectItem = dte.Solution.FindProjectItem(Host.TemplateFile);
Project project = containingProjectItem.ContainingProject;
var projectName = project.FullName;
ProjectItem deploymentConfiguration = GetProjectItem(project, "AppInfo.cs");

if (deploymentConfiguration == null) {
    throw new Exception("Unable to resolve AppInfo.cs");
}

var codeModel = deploymentConfiguration.FileCodeModel;

string id = null;
string product = null;
string company = null;
string version = null;

foreach (CodeElement codeElement in codeModel.CodeElements) {
    if (codeElement.Name == "AppInfo") {
        CodeClass codeClass = codeElement as CodeClass;

        foreach (CodeElement memberElement in codeClass.Members) {
            CodeVariable variable = memberElement as CodeVariable;

            switch (memberElement.Name) {
                case "Id":
                    id = variable.InitExpression as string;
                    break;
                case "Product":
                    product = variable.InitExpression as string;
                    break;
                case "Company":
                    company = variable.InitExpression as string;
                    break;
                case "Version":
                    version = variable.InitExpression as string;
                    break;
             }
        }
    }
} 
#>
<#+ EnvDTE.ProjectItem GetProjectItem(Project project, string fileName)
{
    foreach (ProjectItem projectItem in project.ProjectItems) {
        if (projectItem.Name.EndsWith(fileName)) {
            return projectItem;
        }

        var item = GetProjectItem(projectItem, fileName);

        if (item != null) {
            return item;
        }
    }

    return null;
}

EnvDTE.ProjectItem GetProjectItem(EnvDTE.ProjectItem projectItem, string fileName)
{
    if (projectItem.ProjectItems != null && projectItem.ProjectItems.Count > 0) {
        foreach (ProjectItem item in projectItem.ProjectItems) {
            if (item.Name.EndsWith(fileName)) {
                return item;
            }
        }
    }

    return null;
} 
#>

......,最后是我的示范:

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".xml" #>
<#@ include file="AppInfo.ttinclude" #>
<?xml version="1.0" encoding="utf-8"?>
<Deployment xmlns="http://schemas.microsoft.com/windowsphone/2009/deployment" AppPlatformVersion="7.0">
    <App xmlns="" ProductID="<#= id #>" Title="<#= product #>" RuntimeType="XNA" Version="<#= version #>" Genre="apps.games" Author="Gonzo" Description="<#= description #>" Publisher="<#= company #>">
    <IconPath IsRelative="true" IsResource="false">
    ...
  </App>
</Deployment>

能否用像我国这样的类别来填写清单模板? 我不想重复在议会Info.cs中已经定义的固定不变,即,设想是把出版我从那里获得的劳动合同时所需的所有信息推算出来。

Any idea would be really welcome :-)

j3d

问题回答

同样,我有两种不同的部署配置,分别用于我的评估(专刊或版),以及每个组合,即需要具体的指南、名称、版本等。 我得以通过修改WMAppManifest来解决我的问题。 与此类似:

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".xml" #>
<#@ assembly name="$(TargetPath)" #>
<?xml version="1.0" encoding="utf-8"?>
<Deployment xmlns="http://schemas.microsoft.com/windowsphone/2009/deployment" AppPlatformVersion="7.0">
    <App xmlns="" ProductID="<#= AppInfo.Id #>" Title="<#= AppInfo.Product + " " + AppInfo.Edition #>" RuntimeType="XNA" Version="<#= AppInfo.Version #>" Genre="apps.games" Author="Gonzo" Description="<#= AppInfo.Description #>" Publisher="<#= AppInfo.Company #>">
        ...
    </App>
</Deployment>

In the code snippet here above I specified $(TargetPath) in the assembly directive (this points to my compiled assembly in the bin directory) and eventually I was able to peek custom attributes from AssemblyInfo. In this way, I just defines Guids and other needed data only once - here is piece of my AssemblyInfo:

...
#if !LITE_EDITION
[assembly: Guid("716ab9de-f88e-9a14-8d81-65sn67dbf99e")]
[assembly: AssemblyEdition("Pro")]
#else
[assembly: Guid("91e6742f-6273-1a8b-55a69-e70ad5644827")]
[assembly: AssemblyEdition("Lite")]
#endif
...

根据目前的配置(例如,Debug、Debug Lite、释放、释放Lite),我用正确的信息生成一个信号。 我希望能够提供帮助。

j3d





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签